Class Net::SSH::Multi::Server
In: lib/net/ssh/multi/server.rb
Parent: Object

Encapsulates the connection information for a single remote server, as well as the Net::SSH session corresponding to that information. You‘ll rarely need to instantiate one of these directly: instead, you should use Net::SSH::Multi::Session#use.

Methods

<=>   []   []=   busy?   close   fail!   failed?   hash   inspect   new   port   session   to_s  

Included Modules

Comparable

External Aliases

== -> eql?

Attributes

gateway  [R]  The Net::SSH::Gateway instance to use to establish the connection. Will be nil if the connection should be established without a gateway.
host  [R]  The host name (or IP address) of the server to connect to.
master  [R]  The Net::SSH::Multi::Session instance that manages this server instance.
options  [R]  The Hash of additional options to pass to Net::SSH when connecting (including things like :password, and so forth).
user  [R]  The user name to use when logging into the server.

Public Class methods

Creates a new Server instance with the given connection information. The master argument must be a reference to the Net::SSH::Multi::Session instance that will manage this server reference. The options hash must conform to the options described for Net::SSH::start, with two additions:

  • :via => a Net::SSH::Gateway instance to use when establishing a connection to this server.
  • :user => the name of the user to use when logging into this server.

The host argument may include the username and port number, in which case those values take precedence over similar values given in the options:

  server = Net::SSH::Multi::Server.new(session, 'user@host:1234')
  puts server.user #-> user
  puts server.port #-> 1234

[Source]

    # File lib/net/ssh/multi/server.rb, line 43
43:     def initialize(master, host, options={})
44:       @master = master
45:       @options = options.dup
46: 
47:       @user, @host, port = host.match(/^(?:([^;,:=]+)@|)(.*?)(?::(\d+)|)$/)[1,3]
48: 
49:       user_opt, port_opt = @options.delete(:user), @options.delete(:port)
50: 
51:       @user = @user || user_opt || master.default_user
52:       port ||= port_opt
53: 
54:       @options[:port] = port.to_i if port
55: 
56:       @gateway = @options.delete(:via)
57:       @failed = false
58:     end

Public Instance methods

Gives server definitions a sort order, and allows comparison.

[Source]

    # File lib/net/ssh/multi/server.rb, line 79
79:     def <=>(server)
80:       [host, port, user] <=> [server.host, server.port, server.user]
81:     end

Returns the value of the server property with the given key. Server properties are described via the +:properties+ key in the options hash when defining the Server.

[Source]

    # File lib/net/ssh/multi/server.rb, line 63
63:     def [](key)
64:       (options[:properties] || {})[key]
65:     end

Sets the given key/value pair in the +:properties+ key in the options hash. If the options hash has no :properties key, it will be created.

[Source]

    # File lib/net/ssh/multi/server.rb, line 69
69:     def []=(key, value)
70:       (options[:properties] ||= {})[key] = value
71:     end

Returns true if the session has been opened, and the session is currently busy (as defined by Net::SSH::Connection::Session#busy?).

[Source]

     # File lib/net/ssh/multi/server.rb, line 143
143:     def busy?(include_invisible=false)
144:       session && session.busy?(include_invisible)
145:     end

Closes this server‘s session. If the session has not yet been opened, this does nothing.

[Source]

     # File lib/net/ssh/multi/server.rb, line 149
149:     def close
150:       session.close if session
151:     ensure
152:       master.server_closed(self) if session
153:       @session = nil
154:     end

Indicates (by default) that this server has just failed a connection attempt. If flag is false, this can be used to reset the failed flag so that a retry may be attempted.

[Source]

     # File lib/net/ssh/multi/server.rb, line 115
115:     def fail!(flag=true)
116:       @failed = flag
117:     end

Returns true if this server has ever failed a connection attempt.

[Source]

     # File lib/net/ssh/multi/server.rb, line 108
108:     def failed?
109:       @failed
110:     end

Generates a Fixnum hash value for this object. This function has the property that +a.eql?(b)+ implies +a.hash == b.hash+. The hash value is used by class Hash. Any hash value that exceeds the capacity of a Fixnum will be truncated before being used.

[Source]

    # File lib/net/ssh/multi/server.rb, line 89
89:     def hash
90:       @hash ||= [host, user, port].hash
91:     end

Returns a human-readable representation of this server instance.

[Source]

     # File lib/net/ssh/multi/server.rb, line 103
103:     def inspect
104:       @inspect ||= "#<%s:0x%x %s>" % [self.class.name, object_id, to_s]
105:     end

Returns the port number to use for this connection.

[Source]

    # File lib/net/ssh/multi/server.rb, line 74
74:     def port
75:       options[:port] || 22
76:     end

Returns the Net::SSH session object for this server. If require_session is false and the session has not previously been created, this will return nil. If require_session is true, the session will be instantiated if it has not already been instantiated, via the gateway if one is given, or directly (via Net::SSH::start) otherwise.

  if server.session.nil?
    puts "connecting..."
    server.session(true)
  end

Note that the sessions returned by this are "enhanced" slightly, to make them easier to deal with in a multi-session environment: they have a :server property automatically set on them, that refers to this object (the Server instance that spawned them).

  assert_equal server, server.session[:server]

[Source]

     # File lib/net/ssh/multi/server.rb, line 136
136:     def session(require_session=false)
137:       return @session if @session || !require_session
138:       @session ||= master.next_session(self)
139:     end

Returns a human-readable representation of this server instance.

[Source]

     # File lib/net/ssh/multi/server.rb, line 94
 94:     def to_s
 95:       @to_s ||= begin
 96:         s = "#{user}@#{host}"
 97:         s << ":#{options[:port]}" if options[:port]
 98:         s
 99:       end
100:     end

[Validate]