Class: SwitchTower::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/switchtower/ssh.rb

Overview

A helper class for dealing with SSH connections.

Class Method Summary collapse

Class Method Details

.connect(server, config, port = 22, &block) ⇒ Object

An abstraction to make it possible to connect to the server via public key without prompting for the password. If the public key authentication fails this will fall back to password authentication.

If a block is given, the new session is yielded to it, otherwise the new session is returned.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/switchtower/ssh.rb', line 22

def self.connect(server, config, port=22, &block)
  methods = [ %w(publickey hostbased), %w(password keyboard-interactive) ]
  password_value = nil

  begin
    ssh_options = { :username => config.user,
                    :password => password_value,
                    :port => port,
                    :auth_methods => methods.shift }.merge(config.ssh_options)
    Net::SSH.start(server,ssh_options,&block)
  rescue Net::SSH::AuthenticationFailed
    raise if methods.empty?
    password_value = config.password
    retry
  end
end