Skip to content

Instantly share code, notes, and snippets.

@toamitkumar
Created April 18, 2011 18:15
Show Gist options
  • Save toamitkumar/925850 to your computer and use it in GitHub Desktop.
Save toamitkumar/925850 to your computer and use it in GitHub Desktop.
module Capistrano
class SSH
class << self
alias :original_connect :connect
def connect(server, options={}, &block)
additional_server_settings = options[:server_authentication] ? options[:server_authentication] =~ /#{server.to_s}/ : nil
if additional_server_settings.nil?
return original_connect(server, options, &block)
else
methods = [ %w(publickey hostbased), %w(password keyboard-interactive) ]
password_value = nil
ssh_options = (options[:ssh_options] || {}).dup
port = server.port || options[:port] || ssh_options[:port]
ssh_options[:port] = port if(port)
begin
connection_options = ssh_options.merge(
:password => password_value,
:auth_methods => ssh_options[:auth_methods] || methods.shift
)
connection = Net::SSH.start(server.host, server.user, connection_options, &block)
Server.apply_to(connection, server)
rescue Net::SSH::AuthenticationFailed
raise if methods.empty? || ssh_options[:auth_methods]
password_value = Capistrano::CLI.password_prompt
retry
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment