module ESSH

Constants

VALID_OPTIONS
VERSION

Public Class Methods

assign_defaults(options) click to toggle source
# File lib/evented-ssh.rb, line 103
def self.assign_defaults(options)
    if !options[:logger]
        options[:logger] = Logger.new(STDERR)
        options[:logger].level = Logger::FATAL
    end

    options[:password_prompt] ||= ::Net::SSH::Prompt.default(options)

    [:password, :passphrase].each do |key|
        options.delete(key) if options.key?(key) && options[key].nil?
    end
end
configuration_for(host, use_ssh_config) click to toggle source
# File lib/evented-ssh.rb, line 93
def self.configuration_for(host, use_ssh_config)
    files = case use_ssh_config
        when true then ::Net::SSH::Config.expandable_default_files
        when false, nil then return {}
        else Array(use_ssh_config)
        end

    ::Net::SSH::Config.for(host, files)
end
p_start(host, user = nil, **options) click to toggle source

Start using a promise

# File lib/evented-ssh.rb, line 79
def self.p_start(host, user = nil, **options)
    thread = ::Libuv.reactor
    defer = thread.defer
    thread.next_tick do
        begin
            connection = start(host, user, **options)
            defer.resolve(connection)
        rescue Exception => e
            defer.reject(e)
        end
    end
    defer.promise
end
start(host, user = nil, **options) { |connection| ... } click to toggle source
# File lib/evented-ssh.rb, line 25
def self.start(host, user = nil, **options, &block)
    invalid_options = options.keys - VALID_OPTIONS
    if invalid_options.any?
        raise ArgumentError, "invalid option(s): #{invalid_options.join(', ')}"
    end

    assign_defaults(options)
    _sanitize_options(options)

    options[:user] = user if user
    options = configuration_for(host, options.fetch(:config, true)).merge(options)
    host = options.fetch(:host_name, host)

    if options[:non_interactive]
        options[:number_of_password_prompts] = 0
    end

    if options[:verbose]
        options[:logger].level = case options[:verbose]
            when Integer 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 = ::Net::SSH::Authentication::Session.new(transport, options)

    user = options.fetch(:user, user) || Etc.getlogin
    if auth.authenticate("ssh-connection", user, options[:password])
        connection = ::Net::SSH::Connection::Session.new(transport, options)
        if block_given?
            begin
                yield connection
            ensure
                connection.close unless connection.closed?
            end
        else
            return connection
        end
    else
        transport.close
        raise ::Net::SSH::AuthenticationFailed, "Authentication failed for user #{user}@#{host}"
    end
rescue => e
    transport.socket.__send__(:reject, e) if transport
    raise
end

Private Class Methods

_sanitize_options(options) click to toggle source
# File lib/evented-ssh.rb, line 116
def self._sanitize_options(options)
    invalid_option_values = [nil,[nil]]
    unless (options.values & invalid_option_values).empty?
        nil_options = options.select { |_k,v| invalid_option_values.include?(v) }.map(&:first)
        Kernel.warn "#{caller_locations(2, 1)[0]}: Passing nil, or [nil] to Net::SSH.start is deprecated for keys: #{nil_options.join(', ')}"
    end
end