runnel 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/runnel +170 -0
  2. metadata +67 -0
data/bin/runnel ADDED
@@ -0,0 +1,170 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'yaml'
4
+
5
+ HELP = <<TXT
6
+ Runnel is a easy way to manage your ssh tunnels. It relies on autossh
7
+
8
+ Basic Command Line Usage:
9
+ runnel list_running
10
+ runnel start <tunnel>
11
+ runnel start_all
12
+ runnel kill <tunnel>
13
+ runnel kill_all
14
+
15
+ Configuration is read from '~/.runnel/config'
16
+ PID's are stored in ~/.runnel/pids/
17
+
18
+ TXT
19
+
20
+ TCONF_EXAMPLE = <<YAML
21
+ ---
22
+ :socks_proxy:
23
+ :name: My socks proxy for secure browsing on public WiFi
24
+ :mport: 44488 #The autossh monitor port
25
+ :command: -NfD 8080 mysecurebox.net
26
+ :mysql_proxy:
27
+ :name: mySQL proxy for work
28
+ :mport: 44490
29
+ :command: -NfL 3306:localhost:3306 mysqlbox.org
30
+
31
+ YAML
32
+
33
+ class Runnel
34
+ RUNNEL_DIR = ENV['HOME']+"/.runnel"
35
+ PIDS_DIR = "#{RUNNEL_DIR}/pids"
36
+ PIDS_FILES = "#{PIDS_DIR}/*"
37
+ TUNNEL_CONFIG = "#{RUNNEL_DIR}/tunnels.yml"
38
+
39
+
40
+ def self.config
41
+ @config = YAML.load_file(TUNNEL_CONFIG)
42
+ end
43
+
44
+ def self.setup
45
+ puts "mkdir -p #{RUNNEL_DIR} #{PIDS_DIR}"
46
+ `mkdir -p #{RUNNEL_DIR} #{PIDS_DIR}`
47
+ unless File.exists?(TUNNEL_CONFIG)
48
+ puts "Populating an example config"
49
+ File.open(TUNNEL_CONFIG, 'w').puts(TCONF_EXAMPLE)
50
+ end
51
+ puts "Now just update #{TUNNEL_CONFIG}"
52
+ end
53
+
54
+ def self.all
55
+ list = []
56
+ config.each_pair do |k,v|
57
+ list << self.new(k, v)
58
+ end
59
+ list
60
+ end
61
+
62
+ def self.kill(tid)
63
+ create_from_tunnel_id(tid).kill
64
+ end
65
+
66
+ def self.start(tid)
67
+ tunnel = create_from_tunnel_id(tid)
68
+ tunnel.start
69
+ end
70
+
71
+ def self.start_all
72
+ self.all.each do |t|
73
+ t.start unless t.running?
74
+ end
75
+ end
76
+
77
+ def self.kill_all
78
+ self.all.each do |t|
79
+ t.kill if t.running?
80
+ end
81
+ end
82
+
83
+ def self.create_from_tunnel_id(tid)
84
+ self.new(tid, config[tid.to_sym])
85
+ end
86
+
87
+ attr_reader :id, :config
88
+ alias_method :conf, :config
89
+
90
+ def initialize(id, config)
91
+ @id = id
92
+ @config = config
93
+ end
94
+
95
+ def pp_description
96
+ txt = "#{id} - #{conf[:name]}"
97
+ txt += " (Running: #{pid})" if running?
98
+ running? ? green(txt) : red(txt)
99
+ end
100
+
101
+ def running?
102
+ if File.exists?(pid_file)
103
+ if `ps #{pid} | grep autossh`.length == 0
104
+ File.delete(file)
105
+ false
106
+ else
107
+ true
108
+ end
109
+ end
110
+ end
111
+
112
+ def start
113
+ puts "Starting #{conf[:name]}"
114
+ puts "AUTOSSH_PIDFILE=#{pid_file} autossh -M #{conf[:mport]} #{conf[:command]}"
115
+ ENV['AUTOSSH_PIDFILE'] = pid_file
116
+ `autossh -M #{conf[:mport]} #{conf[:command]}`
117
+ end
118
+
119
+ def kill
120
+ if running?
121
+ `kill #{pid}`
122
+ else
123
+ puts "Unable to find a running #{conf[:name]}"
124
+ end
125
+ end
126
+
127
+ private
128
+
129
+ def pid_file
130
+ "#{PIDS_DIR}/#{id}"
131
+ end
132
+
133
+ def pid
134
+ File.read(pid_file).chomp
135
+ end
136
+
137
+ def red(txt)
138
+ "\033[31m#{txt}\033[0m"
139
+ end
140
+
141
+ def green(txt)
142
+ "\033[32m#{txt}\033[0m"
143
+ end
144
+
145
+ end
146
+
147
+ case ARGV[0]
148
+ when "setup"
149
+ Runnel.setup
150
+ when "kill"
151
+ Runnel.kill(ARGV[1])
152
+ when "kill_all"
153
+ Runnel.kill_all
154
+ when "start"
155
+ Runnel.start(ARGV[1])
156
+ when "start_all"
157
+ Runnel.start_all
158
+ when "help"
159
+ puts HELP
160
+ else # List tunnels
161
+ if File.exists?(Runnel::TUNNEL_CONFIG)
162
+ Runnel.all.each do |r|
163
+ puts r.pp_description
164
+ end
165
+ else
166
+ puts "Looks like this is your first time, lets set everything up."
167
+ Runnel.setup
168
+ end
169
+ end
170
+
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: runnel
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Mark Percival
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-19 00:00:00 -07:00
19
+ default_executable: runnel
20
+ dependencies: []
21
+
22
+ description:
23
+ email: mark@mpercival.com
24
+ executables:
25
+ - runnel
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - bin/runnel
32
+ has_rdoc: true
33
+ homepage: https://github.com/mdp/runnel
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.5.2
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: An autossh tunnel manager, written in Ruby
66
+ test_files: []
67
+