Class: SSHClient::Transport::NetSSH

Inherits:
Base
  • Object
show all
Defined in:
lib/ssh_client/transport/net_ssh.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#handle_listeners, #initialize

Constructor Details

This class inherits a constructor from SSHClient::Transport::Base

Instance Attribute Details

#sshObject (readonly)

Returns the value of attribute ssh.



7
8
9
# File 'lib/ssh_client/transport/net_ssh.rb', line 7

def ssh
  @ssh
end

Instance Method Details

#closeObject



45
46
47
# File 'lib/ssh_client/transport/net_ssh.rb', line 45

def close
  @ssh.close if !closed?
end

#closed?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/ssh_client/transport/net_ssh.rb', line 41

def closed?
  ssh.nil? || ssh.closed?
end

#openObject



36
37
38
39
# File 'lib/ssh_client/transport/net_ssh.rb', line 36

def open
  @ssh = Net::SSH.start config.hostname, config.username,
    password: config.password, logger: config.debug? && config.logger
end

#send_message(command, close: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ssh_client/transport/net_ssh.rb', line 9

def send_message(command, close: false)
  open if closed?
  last_read_time = nil

  ssh.open_channel do |channel|
    channel.send_channel_request 'shell' do |ch, success|
      channel.on_data do |c, data|
        handle_listeners :stdout, data
        last_read_time = Time.now
      end

      channel.on_extended_data do |c, data|
        handle_listeners :stderr, data
      end

      channel.send_data "#{command}\n"
    end
  end

  ssh.loop(0.1) do
    read_timeout = Time.now - last_read_time if last_read_time
    ssh.busy? && (!read_timeout || read_timeout < config.read_timeout)
  end

  ssh.close if close
end