156: def self.start(host, user, options={}, &block)
157: invalid_options = options.keys - VALID_OPTIONS
158: if invalid_options.any?
159: raise ArgumentError, "invalid option(s): #{invalid_options.join(', ')}"
160: end
161:
162: options[:user] = user if user
163: options = configuration_for(host, options.fetch(:config, true)).merge(options)
164: host = options.fetch(:host_name, host)
165:
166: if !options.key?(:logger)
167: options[:logger] = Logger.new(STDERR)
168: options[:logger].level = Logger::FATAL
169: end
170:
171: if options[:verbose]
172: options[:logger].level = case options[:verbose]
173: when Fixnum then options[:verbose]
174: when :debug then Logger::DEBUG
175: when :info then Logger::INFO
176: when :warn then Logger::WARN
177: when :error then Logger::ERROR
178: when :fatal then Logger::FATAL
179: else raise ArgumentError, "can't convert #{options[:verbose].inspect} to any of the Logger level constants"
180: end
181: end
182:
183: transport = Transport::Session.new(host, options)
184: auth = Authentication::Session.new(transport, options)
185:
186: user = options.fetch(:user, user)
187: if auth.authenticate("ssh-connection", user, options[:password])
188: connection = Connection::Session.new(transport, options)
189: if block_given?
190: yield connection
191: connection.close
192: else
193: return connection
194: end
195: else
196: transport.close
197: raise AuthenticationFailed, user
198: end
199: end