Class: Net::SSH::Connection::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/makitzo/monkeys/net-ssh.rb

Defined Under Namespace

Classes: ExecStatus

Instance Method Summary collapse

Instance Method Details

#exec2!(command, options = {}) ⇒ Object

Adapted from: stackoverflow.com/questions/3386233/how-to-get-exit-status-with-rubys-netssh-library

FIXME: we’re currently opening a channel per command which strikes me as inefficient and prevents, for example, working directory from persisting across requests.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/makitzo/monkeys/net-ssh.rb', line 44

def exec2!(command, options = {})
  options = {:log => true}.update(options)
  
  status = ExecStatus.new
  status.command = command
  
  ch = open_channel do |channel|
    channel.exec(command) do |ch, success|
      raise "could not execute command: #{command.inspect}" unless success
      
      channel.on_data { |ch, data| status.stdout << data }
      channel.on_extended_data { |ch, type, data| status.stderr << data }
      channel.on_request("exit-status") { |ch,data| status.exit_code = data.read_long }
      channel.on_request("exit-signal") { |ch,data| status.exit_signal = data.read_long }
    end
  end
  
  self.loop
  
  if options[:log] && self[:logger]
    if options[:log].is_a?(String)
      self[:logger].log_command(status, :command => options[:log])
    else
      self[:logger].log_command(status)
    end
  end
  
  status
end