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

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

Instance Method Summary collapse

Instance Method Details

#exec3!(command, o = {}) ⇒ Hash

Monkey patch that adds the exec3! method. It executes a command, waits for the result, and returns the output as a hash indexed with :stdout, :stderr, :exit_code, :exit_signal. Several options are available: :no_log (don’t display anything), :no_output (don’t show command output), :merge_outputs (merge stdout and stderr), :ignore_error (don’t raise an exception if the command execution fails).

Returns:

  • (Hash)

    result Hash stdout, stderr, exit_code, exit_signal of executed command



10
11
12
13
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
49
50
51
52
53
54
55
56
57
# File 'lib/cute/net-ssh-exec3.rb', line 10

def exec3!(command, o = {})
  puts "SSH exec3 on #{host}: #{command}" unless o[:no_log]
  res = {}
  res[:stdout] = ""
  res[:stderr] = ""
  res[:exit_code] = nil
  res[:exit_signal] = nil
  ts = Time::now
  open_channel do |channel|
    channel.exec(command) do |_ch, success|
      unless success
        abort "FAILED: couldn't execute command (ssh.channel.exec)"
      end
      channel.on_data do |_ch,data|
        print data unless o[:no_output]
        res[:stdout]+=data
      end

      channel.on_extended_data do |_ch,_type,data|
        print data unless o[:no_output]
        if o[:merge_outputs]
          res[:stdout]+=data
        else
          res[:stderr]+=data
        end
      end

      channel.on_request("exit-status") do |_ch,data|
        res[:exit_code] = data.read_long
        d = sprintf("%.1f", Time::now - ts)
        puts "EXITCODE: #{res[:exit_code]} (duration: #{d}s)" unless o[:no_log]
      end

      channel.on_request("exit-signal") do |_ch, data|
        res[:exit_signal] = data.read_long
        d = sprintf("%.1f", Time::now - ts)
        puts "EXITSIGNAL: #{res[:exit_signal]} (duration: #{d}s)" unless o[:no_log]
      end
    end
  end
  self.loop
  if res[:exit_code] != 0 and not o[:ignore_error]
    puts "SSH exec3 failed: #{command}"
    pp res
    raise "SSH exec3 failed: #{command}"
  end
  res
end