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