Encapsulates a single session (connection) to a server via SSH.
[R] | host | The name of the host that this session is connected to. |
[R] | options | The hash of options that were used to establish this session. |
[R] | registry | The dependency-injection registry used by this session. |
Create a new SSH session. This method polymorphically accepts a variable number of parameters, as follows:
- 1 parameter: must be the hostname to connect to.
- 2 parameters: must be the hostname, and either the port (as an integer) or the username to connect as.
- 3 parameters: must be the hostname, and either the port (as an integer) and username, or the username and the password.
- 4 parameters: must be the hostname, port, username, and password.
Any scenario above that omits the username assumes that the USER environment variable is set to the user’s name. Any scenario above that omits the password assumes that the user will log in without a password (ie, using a public key). Any scenario above that omits the port number assumes a port number of 22 (the default for SSH).
Any of the above scenarios may also accept a Hash as the last parameter, specifying a list of additional options to be used to initialize the session. (See Net::SSH::Session.add_options).
Alternatively, named parameters may be used, in which case the first parameter is positional and is always the host to connect to, following which you may specify any of the following named parameters (as symbols):
- :port
- :username
- :password
Any additional parameters are treated as options that configure how the connection behaves.
Allowed options are:
- :keys (the list of filenames identifying the user’s keys)
- :host_keys (the list of filenames identifying the host’s keys)
- :auth_methods (a list of authentication methods to use)
- :crypto_backend (defaults to :ossl, and specifies the cryptography backend to use)
- :registry_options (a hash of options to use when creating the registry)
- :container (the registry to use. If not specified, a new registry will be created)
- :verbose (how verbose the logging output should be. Defaults to :warn).
- :log (the name of the file, or the IO object, to which messages will be logged. Defaults to STDERR.)
Also, any options recognized by Net::SSH::Transport::Session may be given, and will be passed through to initialize the transport session.
If a block is given to this method, then it is called with the new session object. The session object is then closed when the block terminates. If a block is not given, then the session object is returned (and must be closed explicitly).
[ show source ]
# File lib/net/ssh/session.rb, line 90 90: def initialize( *args ) 91: @open = false 92: process_arguments( *args ) 93: 94: @registry.define do |b| 95: b.crypto_backend { @crypto_backend } 96: b.transport_host { @host } 97: b.transport_options { @options } 98: 99: b.userauth_keys { @keys } 100: b.userauth_host_keys { @host_keys } 101: b.userauth_method_order { @auth_methods } 102: 103: # Register myself with the registry, so that other services may 104: # access me. 105: b.session( :pipeline => [] ) { self } 106: 107: b.prompter do 108: require 'net/ssh/util/prompter' 109: Net::SSH::Util::Prompter.new 110: end 111: 112: b.require 'net/ssh/transport/services', "Net::SSH::Transport" 113: b.require 'net/ssh/connection/services', "Net::SSH::Connection" 114: b.require 'net/ssh/userauth/services', "Net::SSH::UserAuth" 115: 116: b.require 'net/ssh/service/services', "Net::SSH::Service" 117: end 118: 119: userauth = @registry[:userauth][:driver] 120: if userauth.authenticate( "ssh-connection", @username, @password ) 121: @open = true 122: @connection = @registry[:connection][:driver] 123: if block_given? 124: yield self 125: close 126: end 127: else 128: @registry[:transport][:session].close 129: raise AuthenticationFailed, @username 130: end 131: end
Closes the session, if it is open. If it is not open, this does nothing.
[ show source ]
# File lib/net/ssh/session.rb, line 135 135: def close 136: @registry[:transport][:session].close if @open 137: @open = false 138: end
Enters the main communication loop. This processes events occuring over the channel. If a block is given, the loop will continue for as long as the block returns true. Otherwise, the loop continues until there are no more open channels. (See Net::SSH::Connection::Driver#loop).
[ show source ]
# File lib/net/ssh/session.rb, line 159 159: def loop( &block ) 160: sanity_check 161: @connection.loop &block 162: end
Provides convenient access to services that have been registered with the session, such as "process" and "forward".
Usage:
session.forward.local(...)
[ show source ]
# File lib/net/ssh/session.rb, line 170 170: def method_missing( sym, *args, &block ) 171: if args.empty? && block.nil? && @registry[:services].has_key?( sym ) 172: return @registry[:services][ sym ] 173: else 174: super 175: end 176: end
Returns true if the session is currently open.
[ show source ]
# File lib/net/ssh/session.rb, line 141 141: def open? 142: @open 143: end
Opens a new communication channel over the current connection. This returns immediately. The block will be invoked when then the channel has been opened. (See Net::SSH::Connection::Driver#open_channel).
[ show source ]
# File lib/net/ssh/session.rb, line 148 148: def open_channel( type="session", data=nil, &block ) 149: sanity_check 150: channel = @connection.open_channel( type, data ) 151: channel.on_confirm_open &block 152: channel 153: end