class Net::SSH::Connection::Session

Public Instance Methods

exec2!(command, options = {}) click to toggle source

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.

# 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