Module: ESSH

Defined in:
lib/evented-ssh.rb,
lib/evented-ssh/version.rb,
lib/evented-ssh/transport/session.rb,
lib/evented-ssh/transport/algorithms.rb,
lib/evented-ssh/transport/packet_stream.rb

Defined Under Namespace

Modules: Transport

Constant Summary collapse

VALID_OPTIONS =
[
  :auth_methods, :bind_address, :compression, :compression_level, :config,
  :encryption, :forward_agent, :hmac, :host_key, :remote_user,
  :keepalive, :keepalive_interval, :keepalive_maxcount, :kex, :keys, :key_data,
  :languages, :logger, :paranoid, :password, :port, :proxy,
  :rekey_blocks_limit,:rekey_limit, :rekey_packet_limit, :timeout, :verbose,
  :known_hosts, :global_known_hosts_file, :user_known_hosts_file, :host_key_alias,
  :host_name, :user, :properties, :passphrase, :keys_only, :max_pkt_size,
  :max_win_size, :send_env, :use_agent, :number_of_password_prompts,
  :append_all_supported_algorithms, :non_interactive, :password_prompt,
  :agent_socket_factory, :minimum_dh_bits
]
VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.assign_defaults(options) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



93
94
95
96
97
98
99
100
101
# 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) ⇒ Object

Start using a promise



79
80
81
82
83
84
85
86
87
88
89
90
91
# 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, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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