class SSHKit::Interactive::Command

Attributes

host[R]
remote_command[R]

Public Class Methods

new(host, remote_command = nil, options = {}) click to toggle source

Instantiate new interactive SSHKit command wrapper.

@param host [SSHKit::Host] the host to run `remote_command` on. @param remote_command [SSHKit::Command] the command to run on `host`.

# File lib/sshkit/interactive/command.rb, line 10
def initialize(host, remote_command = nil, options = {})
  @host           = host
  @remote_command = remote_command
  @options        = options
end

Public Instance Methods

execute() click to toggle source

Run the command on the remote host via SSH binary.

# File lib/sshkit/interactive/command.rb, line 17
def execute
  system(to_s)
end
ssh_cmd_args() click to toggle source

SSH command arguments

# File lib/sshkit/interactive/command.rb, line 22
def ssh_cmd_args
  args = []

  args << '-t'
  args << '-A' if forward_agent?
  args << "-p #{port}" if port
  args << "-l #{user}" if user
  args << %Q{-o "PreferredAuthentications #{auth_methods.join(',')}"} if auth_methods.count > 0
  args << %Q{-o "ProxyCommand #{proxy_command}"} if proxy
  args << %Q{-o "HostName #{host_name}"} if host_name

  keys.each do |key|
    args << "-i #{key}"
  end

  args
end
to_s() click to toggle source

Complete command

# File lib/sshkit/interactive/command.rb, line 41
def to_s
  [
    :ssh,
    *ssh_cmd_args,
    host.hostname,
    command
  ].reject(&:empty?).join(' ')
end

Private Instance Methods

auth_methods() click to toggle source
# File lib/sshkit/interactive/command.rb, line 77
def auth_methods
  Array(netssh_options[:auth_methods])
end
command() click to toggle source
# File lib/sshkit/interactive/command.rb, line 89
def command
  cmd   = remote_command.to_command.gsub("'", "\\\"") # replace single quotes with double quotes
  shell = @options[:shell] || '$SHELL'

  %Q{'#{shell} -l -c \"#{cmd}\"'}
end
forward_agent?() click to toggle source
# File lib/sshkit/interactive/command.rb, line 65
def forward_agent?
  !!netssh_options[:forward_agent]
end
host_name() click to toggle source
# File lib/sshkit/interactive/command.rb, line 69
def host_name
  netssh_options[:host_name]
end
keys() click to toggle source
# File lib/sshkit/interactive/command.rb, line 73
def keys
  Array(netssh_options[:keys])
end
netssh_options() click to toggle source

available options: net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start

# File lib/sshkit/interactive/command.rb, line 53
def netssh_options
  host.netssh_options
end
port() click to toggle source
# File lib/sshkit/interactive/command.rb, line 61
def port
  netssh_options[:port]
end
proxy() click to toggle source
# File lib/sshkit/interactive/command.rb, line 81
def proxy
  netssh_options[:proxy]
end
proxy_command() click to toggle source
# File lib/sshkit/interactive/command.rb, line 85
def proxy_command
  proxy.command_line_template
end
user() click to toggle source
# File lib/sshkit/interactive/command.rb, line 57
def user
  host.user
end