Class: SSHKit::Interactive::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/sshkit/interactive/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, remote_command = nil) ⇒ Command

remote_command can be an SSHKit::Command or a String



7
8
9
10
# File 'lib/sshkit/interactive/command.rb', line 7

def initialize(host, remote_command=nil)
  @host = host
  @remote_command = remote_command
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



4
5
6
# File 'lib/sshkit/interactive/command.rb', line 4

def host
  @host
end

#remote_commandObject (readonly)

Returns the value of attribute remote_command.



4
5
6
# File 'lib/sshkit/interactive/command.rb', line 4

def remote_command
  @remote_command
end

Instance Method Details

#executeObject



64
65
66
# File 'lib/sshkit/interactive/command.rb', line 64

def execute
  system(self.to_s)
end

#hostnameObject



20
21
22
# File 'lib/sshkit/interactive/command.rb', line 20

def hostname
  self.host.hostname
end

#netssh_optionsObject



12
13
14
# File 'lib/sshkit/interactive/command.rb', line 12

def netssh_options
  self.host.netssh_options
end

#optionsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sshkit/interactive/command.rb', line 24

def options
  opts = []
  opts << '-A' if netssh_options[:forward_agent]
  if netssh_options[:keys]
    netssh_options[:keys].each do |k|
      opts << "-i #{k}"
    end
  end
  opts << "-l #{user}" if user
  opts << %{-o "PreferredAuthentications #{netssh_options[:auth_methods].join(',')}"} if netssh_options[:auth_methods]
  opts << %{-o "ProxyCommand #{netssh_options[:proxy].command_line_template}"} if netssh_options[:proxy]
  opts << "-p #{netssh_options[:port]}" if netssh_options[:port]
  opts << '-t' if self.remote_command

  opts
end

#options_strObject



41
42
43
# File 'lib/sshkit/interactive/command.rb', line 41

def options_str
  self.options.join(' ')
end

#remote_command_strObject



45
46
47
48
49
50
51
# File 'lib/sshkit/interactive/command.rb', line 45

def remote_command_str
  if self.remote_command
    %{"#{self.remote_command}"}
  else
    ''
  end
end

#to_sObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/sshkit/interactive/command.rb', line 53

def to_s
  parts = [
    'ssh',
    self.options_str,
    self.hostname,
    self.remote_command_str
  ]

  parts.reject(&:empty?).join(' ')
end

#userObject



16
17
18
# File 'lib/sshkit/interactive/command.rb', line 16

def user
  self.host.user
end