Method: Open4ssh.capture

Defined in:
lib/open4ssh.rb

.capture(host: '', user: '', port: 22, key: '', pwd: '', cmd: '') ⇒ String

Executes a shell command on a remote host via SSH and captures the console output.

Examples:

stdout = Open4ssh.capture(
    host: 'remote.host.io',
    user: 'nane',
    pwd: 'secret',
    cmd: 'ls -la'
)
puts stdout

Parameters:

  • host (String) (defaults to: '')

    DNS name or IP address of the remote host (required)

  • port (Integer) (defaults to: 22)

    Port (defaults to 22)

  • user (String) (defaults to: '')

    User name (required)

  • key (Path) (defaults to: '')

    Path to a key file (.pem) if user logs in via keyfile (not required if password is provided)

  • pwd (String) (defaults to: '')

    Password of user (not required if key is provided)

  • cmd (String) (defaults to: '')

    valid shell command string to be executed on host (required)

Returns:

  • (String)

    console output of executed command (output includes stdout and stderr)

Raises:

  • (One of Net::SSH::Exceptions)

    In case of net::ssh errors due to connection problems, authentication errors, timeouts, .…



51
52
53
54
55
56
57
58
59
60
# File 'lib/open4ssh.rb', line 51

def self.capture(host: '', user: '', port: 22, key: '', pwd: '', cmd: '')
  stdout = ""
  keys = [key]

  Net::SSH.start(host, user, port: port, password: pwd, keys: keys, paranoid: false) do |ssh|
    stdout = ssh.exec!(cmd)
  end

  return stdout
end