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