def self.start(host, user, options={}, &block)
invalid_options = options.keys - VALID_OPTIONS
if invalid_options.any?
raise ArgumentError, "invalid option(s): #{invalid_options.join(', ')}"
end
files = case options.fetch(:config, true)
when true then Net::SSH::Config.default_files
when false, nil then []
else Array(options[:config])
end
options = Net::SSH::Config.for(host, files).merge(options)
host = options.fetch(:host_name, host)
if !options.key?(:logger)
options[:logger] = Logger.new(STDERR)
options[:logger].level = Logger::FATAL
end
if options[:verbose]
options[:logger].level = case options[:verbose]
when Fixnum then options[:verbose]
when :debug then Logger::DEBUG
when :info then Logger::INFO
when :warn then Logger::WARN
when :error then Logger::ERROR
when :fatal then Logger::FATAL
else raise ArgumentError, "can't convert #{options[:verbose].inspect} to any of the Logger level constants"
end
end
transport = Transport::Session.new(host, options)
auth = Authentication::Session.new(transport, options)
user = options.fetch(:user, user)
if auth.authenticate("ssh-connection", user, options[:password])
connection = Connection::Session.new(transport, options)
if block_given?
yield connection
connection.close
else
return connection
end
else
raise AuthenticationFailed, user
end
end