Class Net::SSH::Gateway
In: lib/net/ssh/gateway.rb
Parent: Object

A Gateway is an object that allows you to tunnel network connections through a publicly visible host to a host hidden behind it. This is particularly useful when dealing with hosts behind a firewall. One host will generally be visible (and accessible) outside the firewall, while the others will all be behind the firewall, and the only way to access those restricted hosts is by first logging into the publicly visible host, and from thence logging into the restricted ones.

This class makes it easy to programmatically connect to these restricted hosts via SSH. You can either simply forward a port from the local host to the remote host, or you can open a new Net::SSH connection to the remote host via a forwarded port.

  require 'net/ssh/gateway'

  gateway = Net::SSH::Gateway.new('host.name', 'user')

  gateway.open('hidden.host', 80) do |port|
    Net::HTTP.get_print '127.0.0.1', '/path', port
  end

  gateway.ssh('hidden.host', 'user') do |ssh|
    puts ssh.exec!("hostname")
  end

  gateway.shutdown!

Port numbers are allocated automatically, beginning at MAX_PORT and decrementing on each request for a new port until MIN_PORT is reached. If a port is already in use, this is detected and a different port will be assigned.

Methods

active?   close   new   open   shutdown!   ssh  

Classes and Modules

Class Net::SSH::Gateway::Version

Constants

MAX_PORT = 65535   The maximum port number that the gateway will attempt to use to forward connections from.
MIN_PORT = 1024   The minimum port number that the gateway will attempt to use to forward connections from.

Public Class methods

Instantiate a new Gateway object, using the given remote host as the tunnel. The arguments here are identical to those for Net::SSH.start, and are passed as given to that method to start up the gateway connection.

  gateway = Net::SSH::Gateway.new('host', 'user', :password => "password")

[Source]

    # File lib/net/ssh/gateway.rb, line 68
68:   def initialize(host, user, options={})
69:     @session = Net::SSH.start(host, user, options)
70:     @session_mutex = Mutex.new
71:     @port_mutex = Mutex.new
72:     @next_port = MAX_PORT
73: 
74:     initiate_event_loop!
75:   end

Public Instance methods

Returns true if the gateway is currently open and accepting connections. This will be the case unless shutdown! has been invoked.

[Source]

    # File lib/net/ssh/gateway.rb, line 79
79:   def active?
80:     @active
81:   end

Cancels port-forwarding over an open port that was previously opened via open.

[Source]

     # File lib/net/ssh/gateway.rb, line 138
138:   def close(port)
139:     ensure_open!
140: 
141:     @session_mutex.synchronize do
142:       @session.forward.cancel_local(port)
143:     end
144:   end

Opens a new port on the local host and forwards it to the given host/port via the gateway host. If a block is given, the newly allocated port number will be yielded to the block, and the port automatically closed (see close) when the block finishes. Otherwise, the port number will be returned, and the caller is responsible for closing the port (close).

  gateway.open('host', 80) do |port|
    # ...
  end

  port = gateway.open('host', 80)
  # ...
  gateway.close(port)

[Source]

     # File lib/net/ssh/gateway.rb, line 114
114:   def open(host, port)
115:     ensure_open!
116: 
117:     local_port = next_port
118: 
119:     @session_mutex.synchronize do
120:       @session.forward.local(local_port, host, port)
121:     end
122: 
123:     if block_given?
124:       begin
125:         yield local_port
126:       ensure
127:         close(local_port)
128:       end
129:     else
130:       return local_port
131:     end
132:   rescue Errno::EADDRINUSE
133:     retry
134:   end

Shuts down the gateway by closing all forwarded ports and then closing the gateway‘s SSH session.

[Source]

    # File lib/net/ssh/gateway.rb, line 85
85:   def shutdown!
86:     return unless active?
87: 
88:     @session_mutex.synchronize do
89:       # cancel all active forward channels
90:       @session.forward.active_locals.each do |lport, host, port|
91:         @session.forward.cancel_local(lport)
92:       end
93:     end
94: 
95:     @active = false
96:     
97:     @thread.join
98:     @session.close
99:   end

Forwards a new connection to the given host and opens a new Net::SSH connection to that host over the forwarded port. If a block is given, the new SSH connection will be yielded to the block, and autoclosed when the block terminates. The forwarded port will be autoclosed as well. If no block was given, the new SSH connection will be returned, and it is up to the caller to terminate both the connection and the forwarded port when done.

  gateway.ssh('host', 'user') do |ssh|
    # ...
  end

  ssh = gateway.ssh('host', 'user')
  # ...
  ssh.close
  gateway.close(ssh.transport.port)

[Source]

     # File lib/net/ssh/gateway.rb, line 162
162:   def ssh(host, user, options={}, &block)
163:     local_port = open(host, options[:port] || 22)
164: 
165:     begin
166:       Net::SSH.start("127.0.0.1", user, options.merge(:port => local_port), &block)
167:     ensure
168:       close(local_port) if block || $!
169:     end
170:   end

[Validate]