class Shells::SshShell

Executes an SSH session with a host.

Valid options:

host

The name or IP address of the host to connect to. Defaults to 'localhost'.

port

The port on the host to connect to. Defaults to 22.

user

The user to login with. This option is required.

password

The password to login with. If our public key is an authorized key on the host, the password is ignored for connection. The sudo_exec method for bash-like shells will also use this password for elevation.

prompt

The prompt used to determine when processes finish execution.

shell

If set to :shell, then the default shell is executed. This is the default value. If set to :none, then no shell is executed, but a PTY is still created. If set to :no_pty, then no shell is executed and no PTY is created. If set to anything else, it is assumed to be the executable path to the shell you want to run.

quit

If set, this defines the command to execute when quitting the session. The default is “exit” which will probably work most of the time.

retrieve_exit_code

If set to a non-false value, then the default behavior will be to retrieve the exit code from the shell after executing a command. If set to a false or nil value, the default behavior will be to ignore the exit code from the shell. When retrieved, the exit code is stored in the last_exit_code property. This option can be overridden by providing an alternate value to the exec method on a case-by-case basis.

on_non_zero_exit_code

If set to :ignore (the default) then non-zero exit codes will not cause errors. You will still be able to check the last_exit_code property to determine if the command was successful. If set to :raise then non-zero exit codes will cause a Shells::NonZeroExitCode to be raised when a command exits with a non-zero return value. This option only comes into play when retrieve_exit_code is set to a non-false value. This option can be overridden by providing an alternate value to the exec method on a case-by-case basis.

silence_timeout

When a command is executing, this is the maximum amount of time to wait for any feedback from the shell. If set to 0 (or less) there is no timeout. Unlike command_timeout this value resets every time we receive feedback. This option can be overridden by providing an alternate value to the exec method on a case-by-case basis.

command_timeout

When a command is executing, this is the maximum amount of time to wait for the command to finish. If set to 0 (or less) there is no timeout. Unlike silence_timeout this value does not reset when we receive feedback. This option can be overridden by providing an alternate value to the exec method on a case-by-case basis.

connect_timeout

This is the maximum amount of time to wait for the initial connection to the SSH shell.

Shells::SshShell.new(
    host: '10.10.10.10',
    user: 'somebody',
    password: 'super-secret'
) do |shell|
  shell.exec('cd /usr/local/bin')
  user_bin_files = shell.exec('ls -A1').split("\n")
  @app_is_installed = user_bin_files.include?('my_app')
end

Attributes

channel[RW]
ssh[RW]

Protected Instance Methods

active?() click to toggle source
# File lib/shells/ssh_shell.rb, line 202
def active?
  channel&.active?
end
io_loop(&block) click to toggle source
# File lib/shells/ssh_shell.rb, line 206
def io_loop(&block)
  shell = self
  ssh&.loop(0.000001) do |_|
    shell.instance_eval &block
  end
end