mirror of
https://github.com/capistrano/capistrano
synced 2023-03-27 23:21:18 -04:00
5246654ab7
git-svn-id: http://svn.rubyonrails.org/rails/trunk/switchtower@2000 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
30 lines
954 B
Ruby
30 lines
954 B
Ruby
require 'net/ssh'
|
|
|
|
module SwitchTower
|
|
# A helper class for dealing with SSH connections.
|
|
class SSH
|
|
# 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.
|
|
def self.connect(server, config, port=22, &block)
|
|
methods = [ %w(publickey hostbased), %w(password keyboard-interactive) ]
|
|
password_value = nil
|
|
|
|
begin
|
|
Net::SSH.start(server,
|
|
:username => config.user,
|
|
:password => password_value,
|
|
:port => port,
|
|
:auth_methods => methods.shift,
|
|
&block)
|
|
rescue Net::SSH::AuthenticationFailed
|
|
raise if methods.empty?
|
|
password_value = config.password
|
|
retry
|
|
end
|
|
end
|
|
end
|
|
end
|