Class: Net::SSH::Transport::Session

### Home Classes Methods

### In Files

- lib/net/ssh/transport/session.rb

### Parent

Object

### Methods

- ::new
- #close
- #closed?
- #configure_client
- #configure_server
- #enqueue_message
- #hint
- #host_as_string
- #next_message
- #peer
- #poll_message
- #push
- #rekey!
- #rekey_as_needed
- #send_message
- #service_request
- #shutdown!
- #wait

### Included Modules

- Net::SSH::Transport::Constants
- Net::SSH::Loggable

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Net
- Net::SSH
- Net::SSH::Authentication
- Net::SSH::Authentication::Agent
- Net::SSH::Authentication::Agent::Comment
- Net::SSH::Authentication::AgentError
- Net::SSH::Authentication::AgentNotAvailable
- Net::SSH::Authentication::Constants
- Net::SSH::Authentication::DisallowedMethod
- Net::SSH::Authentication::KeyManager
- Net::SSH::Authentication::KeyManagerError
- Net::SSH::Authentication::Methods
- Net::SSH::Authentication::Methods::Abstract
- Net::SSH::Authentication::Methods::Hostbased
- Net::SSH::Authentication::Methods::KeyboardInteractive
- Net::SSH::Authentication::Methods::None
- Net::SSH::Authentication::Methods::Password
- Net::SSH::Authentication::Methods::Publickey
- Net::SSH::Authentication::Session
- Net::SSH::AuthenticationFailed
- Net::SSH::Buffer
- Net::SSH::ChannelOpenFailed
- Net::SSH::ChannelRequestFailed
- Net::SSH::Compat
- Net::SSH::Config
- Net::SSH::Connection
- Net::SSH::Connection::Channel
- Net::SSH::Connection::Constants
- Net::SSH::Connection::Session
- Net::SSH::Connection::Session::NilChannel
- Net::SSH::Connection::Term
- Net::SSH::ConnectionTimeout
- Net::SSH::Disconnect
- Net::SSH::Exception
- Net::SSH::HostKeyError
- Net::SSH::HostKeyMismatch
- Net::SSH::HostKeyUnknown
- Net::SSH::KeyFactory
- Net::SSH::KnownHosts
- Net::SSH::Loggable
- Net::SSH::Packet
- Net::SSH::PromptMethods
- Net::SSH::PromptMethods::Clear
- Net::SSH::PromptMethods::Highline
- Net::SSH::PromptMethods::Termios
- Net::SSH::Proxy
- Net::SSH::Proxy::Command
- Net::SSH::Proxy::ConnectError
- Net::SSH::Proxy::Error
- Net::SSH::Proxy::HTTP
- Net::SSH::Proxy::SOCKS4
- Net::SSH::Proxy::SOCKS5
- Net::SSH::Proxy::UnauthorizedError
- Net::SSH::Test
- Net::SSH::Test::Channel
- Net::SSH::Test::Extensions
- Net::SSH::Test::Extensions::BufferedIo
- Net::SSH::Test::Extensions::Channel
- Net::SSH::Test::Extensions::IO
- Net::SSH::Test::Extensions::IO::ClassMethods
- Net::SSH::Test::Extensions::PacketStream
- Net::SSH::Test::Kex
- Net::SSH::Test::LocalPacket
- Net::SSH::Test::Packet
- Net::SSH::Test::RemotePacket
- Net::SSH::Test::Script
- Net::SSH::Test::Socket
- Net::SSH::Transport
- Net::SSH::Transport::Algorithms
- Net::SSH::Transport::CTR
- Net::SSH::Transport::CipherFactory
- Net::SSH::Transport::Constants
- Net::SSH::Transport::HMAC
- Net::SSH::Transport::HMAC::Abstract
- Net::SSH::Transport::HMAC::MD5
- Net::SSH::Transport::HMAC::MD5_96
- Net::SSH::Transport::HMAC::None
- Net::SSH::Transport::HMAC::RIPEMD160
- Net::SSH::Transport::HMAC::SHA1
- Net::SSH::Transport::HMAC::SHA1_96
- Net::SSH::Transport::HMAC::SHA2_256
- Net::SSH::Transport::HMAC::SHA2_256_96
- Net::SSH::Transport::HMAC::SHA2_512
- Net::SSH::Transport::HMAC::SHA2_512_96
- Net::SSH::Transport::IdentityCipher
- Net::SSH::Transport::Kex
- Net::SSH::Transport::Kex::DiffieHellmanGroup14SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroup1SHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA1
- Net::SSH::Transport::Kex::DiffieHellmanGroupExchangeSHA256
- Net::SSH::Transport::Kex::EcdhSHA2NistP256
- Net::SSH::Transport::Kex::EcdhSHA2NistP384
- Net::SSH::Transport::Kex::EcdhSHA2NistP521
- Net::SSH::Transport::KeyExpander
- Net::SSH::Transport::PacketStream
- Net::SSH::Transport::ServerVersion
- Net::SSH::Transport::Session
- Net::SSH::Transport::State
- Net::SSH::Verifiers
- Net::SSH::Verifiers::Lenient
- Net::SSH::Verifiers::Null
- Net::SSH::Verifiers::Secure
- Net::SSH::Verifiers::Strict
- Net::SSH::Version
- OpenSSL
- OpenSSL::BN
- OpenSSL::PKey
- OpenSSL::PKey::DH
- OpenSSL::PKey::DSA
- OpenSSL::PKey::EC
- OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey
- OpenSSL::PKey::OpenSSL::PKey::OpenSSL::PKey::ECError
- OpenSSL::PKey::PKey
- OpenSSL::PKey::RSA
- String

No matching classes.

# 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

algorithms[R]

The Algorithms instance used to perform key exchanges.

host[R]

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

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.

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.

server_version[R]

The ServerVersion instance that encapsulates the negotiated protocol version.

socket[R]

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

### 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 57
    def initialize(host, options={})
      self.logger = options[:logger]

      @host = host
      @port = options[:port] || DEFAULT_PORT
      @bind_address = options[:bind_address] || nil
      @options = options

      debug { "establishing connection to #{@host}:#{@port}" }
      factory = options[:proxy] || TCPSocket
      @socket = timeout(options[:timeout] || 0) { @bind_address.nil? || options[:proxy] ? factory.open(@host, @port) : factory.open(@host,@port,@bind_address) }
      @socket.extend(PacketStream)
      @socket.logger = @logger

      debug { "connection established" }

      @queue = []

      @host_key_verifier = select_host_key_verifier(options[:paranoid])

      @server_version = timeout(options[:timeout] || 0) { ServerVersion.new(socket, logger) }

      @algorithms = Algorithms.new(self, options)
      wait { algorithms.initialized? }
    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 105
    def close
      socket.cleanup
      socket.close
    end

closed?() click to toggle source

Returns true if the underlying socket has been closed.

    # File lib/net/ssh/transport/session.rb, line 100
    def closed?
      socket.closed?
    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 232
    def configure_client(options={})
      socket.client.set(options)
    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 239
    def configure_server(options={})
      socket.server.set(options)
    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 225
    def enqueue_message(message)
      socket.enqueue_packet(message)
    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 245
    def hint(which, value=true)
      socket.hints[which] = value
    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 86
    def host_as_string
      @host_as_string ||= begin
        string = "#{host}"
        string = "[#{string}]:#{port}" if port != DEFAULT_PORT
        if socket.peer_ip != host
          string2 = socket.peer_ip
          string2 = "[#{string2}]:#{port}" if port != DEFAULT_PORT
          string << "," << string2
        end
        string
      end
    end

next_message() click to toggle source

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

    # File lib/net/ssh/transport/session.rb, line 151
    def next_message
      poll_message(:block)
    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 host_as_string).

    # File lib/net/ssh/transport/session.rb, line 145
    def peer
      @peer ||= { :ip => socket.peer_ip, :port => @port.to_i, :host => @host, :canonized => host_as_string }
    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 166
    def poll_message(mode=:nonblock, consume_queue=true)
      loop do
        if consume_queue && @queue.any? && algorithms.allow?(@queue.first)
          return @queue.shift
        end

        packet = socket.next_packet(mode)
        return nil if packet.nil?

        case packet.type
        when DISCONNECT
          raise Net::SSH::Disconnect, "disconnected: #{packet[:description]} (#{packet[:reason_code]})"

        when IGNORE
          debug { "IGNORE packet recieved: #{packet[:data].inspect}" }

        when UNIMPLEMENTED
          lwarn { "UNIMPLEMENTED: #{packet[:number]}" }

        when DEBUG
          send(packet[:always_display] ? :fatal : :debug) { packet[:message] }

        when KEXINIT
          algorithms.accept_kexinit(packet)

        else
          return packet if algorithms.allow?(packet)
          push(packet)
        end
      end
    end

push(packet) click to toggle source

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

    # File lib/net/ssh/transport/session.rb, line 213
    def push(packet)
      @queue.push(packet)
    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 128
    def rekey!
      if !algorithms.pending?
        algorithms.rekey!
        wait { algorithms.initialized? }
      end
    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 138
    def rekey_as_needed
      return if algorithms.pending?
      socket.if_needs_rekey? { rekey! }
    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 219
    def send_message(message)
      socket.send_packet(message)
    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 121
    def service_request(service)
      Net::SSH::Buffer.from(:byte, SERVICE_REQUEST, :string, service)
    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 114
    def shutdown!
      error { "forcing connection closed" }
      socket.close
    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 push).

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.