Class: TunnelVision::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnel_vision.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTunnel

Returns a new instance of Tunnel.



7
8
9
# File 'lib/tunnel_vision.rb', line 7

def initialize
  @pids = []
end

Instance Attribute Details

#pidsObject

Returns the value of attribute pids.



5
6
7
# File 'lib/tunnel_vision.rb', line 5

def pids
  @pids
end

Instance Method Details

#add(tunnel, user, server) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tunnel_vision.rb', line 11

def add tunnel, user, server
  from, host, to = tunnel.split(':')
  pid = fork do
    puts "Connecting #{user}@#{server}"
    begin
      getaway = Net::SSH.start server, user do |ssh|
        puts "setting forwarding (#{from} -> #{to} on #{host})"
        ssh.forward.local from, host, to
        ssh.loop { true }
      end
    rescue => e
      puts ">>> AUTH ERROR <<"
      puts e.inspect
      exit 1
    end
  end
  @pids << pid
  pid
end

#close_all!Object



31
32
33
34
35
36
# File 'lib/tunnel_vision.rb', line 31

def close_all!
  @pids.each do |pid|
    puts "Closing #{pid}"
    `kill -9 #{pid}`
  end
end