Parent

Included Modules

Class Index [+]

Quicksearch

Net::SSH::Transport::Session

The transport layer represents the lowest level of the SSH protocol, and implements basic message exchanging and protocol initialization. It will never be instantiated directly (unless you really know what you’re about), but will instead be created for you automatically when you create a new SSH session via Net::SSH.start.

Constants

DEFAULT_PORT

The standard port for the SSH protocol.

Attributes

host[R]

The host to connect to, as given to the constructor.

port[R]

The port number to connect to, as given in the options to the constructor. If no port number was given, this will default to DEFAULT_PORT.

socket[R]

The underlying socket object being used to communicate with the remote host.

server_version[R]

The ServerVersion instance that encapsulates the negotiated protocol version.

algorithms[R]

The Algorithms instance used to perform key exchanges.

host_key_verifier[R]

The host-key verifier object used to verify host keys, to ensure that the connection is not being spoofed.

options[R]

The hash of options that were given to the object at initialization.

Public Class Methods

new(host, options={}) click to toggle source

Instantiates a new transport layer abstraction. This will block until the initial key exchange completes, leaving you with a ready-to-use transport session.

    # File lib/net/ssh/transport/session.rb, line 56
56:     def initialize(host, options={})
57:       self.logger = options[:logger]
58: 
59:       @host = host
60:       @port = options[:port] || DEFAULT_PORT
61:       @bind_address = options[:bind_address] || nil
62:       @options = options
63: 
64:       debug { "establishing connection to #{@host}:#{@port}" }
65:       factory = options[:proxy] || TCPSocket
66:       @socket = timeout(options[:timeout] || 0) { @bind_address.nil? || options[:proxy] ? factory.open(@host, @port) : factory.open(@host,@port,@bind_address) }
67:       @socket.extend(PacketStream)
68:       @socket.logger = @logger
69: 
70:       debug { "connection established" }
71: 
72:       @queue = []
73: 
74:       @host_key_verifier = select_host_key_verifier(options[:paranoid])
75: 
76: 
77:       @server_version = timeout(options[:timeout] || 0) { ServerVersion.new(socket, logger) }
78: 
79:       @algorithms = Algorithms.new(self, options)
80:       wait { algorithms.initialized? }
81:     end

Public Instance Methods

close() click to toggle source

Cleans up (see PacketStream#cleanup) and closes the underlying socket.

     # File lib/net/ssh/transport/session.rb, line 104
104:     def close
105:       socket.cleanup
106:       socket.close
107:     end
closed?() click to toggle source

Returns true if the underlying socket has been closed.

     # File lib/net/ssh/transport/session.rb, line 99
 99:     def closed?
100:       socket.closed?
101:     end
configure_client(options={}) click to toggle source

Configure’s the packet stream’s client state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when sending packets to the server.

     # File lib/net/ssh/transport/session.rb, line 231
231:     def configure_client(options={})
232:       socket.client.set(options)
233:     end
configure_server(options={}) click to toggle source

Configure’s the packet stream’s server state with the given set of options. This is typically used to define the cipher, compression, and hmac algorithms to use when reading packets from the server.

     # File lib/net/ssh/transport/session.rb, line 238
238:     def configure_server(options={})
239:       socket.server.set(options)
240:     end
enqueue_message(message) click to toggle source

Enqueues the given message, such that it will be sent at the earliest opportunity. This does not block, but returns immediately.

     # File lib/net/ssh/transport/session.rb, line 224
224:     def enqueue_message(message)
225:       socket.enqueue_packet(message)
226:     end
hint(which, value=true) click to toggle source

Sets a new hint for the packet stream, which the packet stream may use to change its behavior. (See PacketStream#hints).

     # File lib/net/ssh/transport/session.rb, line 244
244:     def hint(which, value=true)
245:       socket.hints[which] = value
246:     end
host_as_string() click to toggle source

Returns the host (and possibly IP address) in a format compatible with SSH known-host files.

    # File lib/net/ssh/transport/session.rb, line 85
85:     def host_as_string
86:       @host_as_string ||= begin
87:         string = "#{host}"
88:         string = "[#{string}]:#{port}" if port != DEFAULT_PORT
89:         if socket.peer_ip != host
90:           string2 = socket.peer_ip
91:           string2 = "[#{string2}]:#{port}" if port != DEFAULT_PORT
92:           string << "," << string2
93:         end
94:         string
95:       end
96:     end
next_message() click to toggle source

Blocks until a new packet is available to be read, and returns that packet. See #.

     # File lib/net/ssh/transport/session.rb, line 150
150:     def next_message
151:       poll_message(:block)
152:     end
peer() click to toggle source

Returns a hash of information about the peer (remote) side of the socket, including :ip, :port, :host, and :canonized (see #).

     # File lib/net/ssh/transport/session.rb, line 144
144:     def peer
145:       @peer ||= { :ip => socket.peer_ip, :port => @port.to_i, :host => @host, :canonized => host_as_string }
146:     end
poll_message(mode=:nonblock, consume_queue=true) click to toggle source

Tries to read the next packet from the socket. If mode is :nonblock (the default), this will not block and will return nil if there are no packets waiting to be read. Otherwise, this will block until a packet is available. Note that some packet types (DISCONNECT, IGNORE, UNIMPLEMENTED, DEBUG, and KEXINIT) are handled silently by this method, and will never be returned.

If a key-exchange is in process and a disallowed packet type is received, it will be enqueued and otherwise ignored. When a key-exchange is not in process, and consume_queue is true, packets will be first read from the queue before the socket is queried.

     # File lib/net/ssh/transport/session.rb, line 165
165:     def poll_message(mode=:nonblock, consume_queue=true)
166:       loop do
167:         if consume_queue && @queue.any? && algorithms.allow?(@queue.first)
168:           return @queue.shift
169:         end
170: 
171:         packet = socket.next_packet(mode)
172:         return nil if packet.nil?
173: 
174:         case packet.type
175:         when DISCONNECT
176:           raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})"
177: 
178:         when IGNORE
179:           debug { "IGNORE packet recieved: #{packet[:data].inspect}" }
180: 
181:         when UNIMPLEMENTED
182:           lwarn { "UNIMPLEMENTED: #{packet[:number]}" }
183: 
184:         when DEBUG
185:           send(packet[:always_display] ? :fatal : :debug) { packet[:message] }
186: 
187:         when KEXINIT
188:           algorithms.accept_kexinit(packet)
189: 
190:         else
191:           return packet if algorithms.allow?(packet)
192:           push(packet)
193:         end
194:       end
195:     end
push(packet) click to toggle source

Adds the given packet to the packet queue. If the queue is non-empty, # will return packets from the queue in the order they were received.

     # File lib/net/ssh/transport/session.rb, line 212
212:     def push(packet)
213:       @queue.push(packet)
214:     end
rekey!() click to toggle source

Requests a rekey operation, and blocks until the operation completes. If a rekey is already pending, this returns immediately, having no effect.

     # File lib/net/ssh/transport/session.rb, line 127
127:     def rekey!
128:       if !algorithms.pending?
129:         algorithms.rekey!
130:         wait { algorithms.initialized? }
131:       end
132:     end
rekey_as_needed() click to toggle source

Returns immediately if a rekey is already in process. Otherwise, if a rekey is needed (as indicated by the socket, see PacketStream#if_needs_rekey?) one is performed, causing this method to block until it completes.

     # File lib/net/ssh/transport/session.rb, line 137
137:     def rekey_as_needed
138:       return if algorithms.pending?
139:       socket.if_needs_rekey? { rekey! }
140:     end
send_message(message) click to toggle source

Sends the given message via the packet stream, blocking until the entire message has been sent.

     # File lib/net/ssh/transport/session.rb, line 218
218:     def send_message(message)
219:       socket.send_packet(message)
220:     end
service_request(service) click to toggle source

Returns a new service_request packet for the given service name, ready for sending to the server.

     # File lib/net/ssh/transport/session.rb, line 120
120:     def service_request(service)
121:       Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service)
122:     end
shutdown!() click to toggle source

Performs a “hard” shutdown of the connection. In general, this should never be done, but it might be necessary (in a rescue clause, for instance, when the connection needs to close but you don’t know the status of the underlying protocol’s state).

     # File lib/net/ssh/transport/session.rb, line 113
113:     def shutdown!
114:       error { "forcing connection closed" }
115:       socket.close
116:     end
wait() click to toggle source

Waits (blocks) until the given block returns true. If no block is given, this just waits long enough to see if there are any pending packets. Any packets read are enqueued (see #).

     # File lib/net/ssh/transport/session.rb, line 200
200:     def wait
201:       loop do
202:         break if block_given? && yield
203:         message = poll_message(:nonblock, false)
204:         push(message) if message
205:         break if !block_given?
206:       end
207:     end

Private Instance Methods

select_host_key_verifier(paranoid) click to toggle source

Instantiates a new host-key verification class, based on the value of the parameter. When true or nil, the default Lenient verifier is returned. If it is false, the Null verifier is returned, and if it is :very, the Strict verifier is returned. If the argument happens to respond to :verify, it is returned directly. Otherwise, an exception is raised.

     # File lib/net/ssh/transport/session.rb, line 261
261:       def select_host_key_verifier(paranoid)
262:         case paranoid
263:         when true, nil then
264:           Net::SSH::Verifiers::Lenient.new
265:         when false then
266:           Net::SSH::Verifiers::Null.new
267:         when :very then
268:           Net::SSH::Verifiers::Strict.new
269:         else
270:           if paranoid.respond_to?(:verify)
271:             paranoid
272:           else
273:             raise ArgumentError, "argument to :paranoid is not valid: #{paranoid.inspect}"
274:           end
275:         end
276:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.