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

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

Overview

Grab exit codes from remote commands Yanked this from stackoverflow.com/a/13436242

Defined Under Namespace

Classes: CommandExecutionFailed, CommandFailed

Instance Method Summary collapse

Instance Method Details

#exec_sc!(command) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stackadmin/net-ssh.rb', line 14

def exec_sc!(command)
  stdout_data, stderr_data = '', ''
  exit_code, exit_signal = nil, nil
  self.open_channel do |channel|
    channel.exec(command) do |_, success|
      raise CommandExecutionFailed, "Command \"#{command}\" was unable to execute" unless success

      channel.on_data do |_, data|
        stdout_data += data
      end

      channel.on_extended_data do |_, _, data|
        stderr_data += data
      end

      channel.on_request('exit-status') do |_, data|
        exit_code = data.read_long
      end

      channel.on_request('exit-signal') do |_, data|
        exit_signal = data.read_long
      end
    end
  end
  self.loop

  raise CommandFailed, "Command \"#{command}\" returned exit code #{exit_code}" unless exit_code == 0

  {
    stdout: stdout_data,
    stderr: stderr_data,
    exit_code: exit_code,
    exit_signal: exit_signal
  }
end