32

I have a snippet of code, simply trying to execute a script on a remote server, in the event that it fails, I'd like to make a follow-up call, imagine this:

require 'rubygems'
require 'net/ssh'
require 'etc'

server = 'localhost'

Net::SSH.start(server, Etc.getlogin) do |ssh|
  puts (ssh.exec("true")  ? 'Exit Success' : "Exit Failure")
  puts (ssh.exec("false") ? 'Exit Success' : "Exit Failure")  
end

I would expect (ignoring that stdout and stderr are printed in my contrived example) - but first line should exit with 0 which I would expect Ruby would interperate as false and display "Exit Failure" (sure, so the logic is wrong, the ternary needs to be flipped) - but the second line should exit with the opposite status, and it doesn't.

I can't even find anything in the documentation about how to do this, and I'm a little worried that I might be doing it wrong?!

Lee Hambley
  • 6,270
  • 5
  • 49
  • 81

3 Answers3

77

I find the following way of running processes with Net::SSH much more useful. It provides you with distinct stdout and stderr, exit code and exit signal.

require 'rubygems'
require 'net/ssh'
require 'etc'

server = 'localhost'

def ssh_exec!(ssh, command)
  stdout_data = ""
  stderr_data = ""
  exit_code = nil
  exit_signal = nil
  ssh.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|
        stdout_data+=data
      end

      channel.on_extended_data do |ch,type,data|
        stderr_data+=data
      end

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

      channel.on_request("exit-signal") do |ch, data|
        exit_signal = data.read_long
      end
    end
  end
  ssh.loop
  [stdout_data, stderr_data, exit_code, exit_signal]
end

Net::SSH.start(server, Etc.getlogin) do |ssh|
  puts ssh_exec!(ssh, "true").inspect
  # => ["", "", 0, nil]

  puts ssh_exec!(ssh, "false").inspect  
  # => ["", "", 1, nil]

end

Hope this helps.

Han
  • 5,374
  • 5
  • 31
  • 31
flitzwald
  • 20,200
  • 2
  • 32
  • 28
  • flitzwald, that's awesome - I wish I could retrospectively add a bounty or something! Thanks a lot! – Lee Hambley Aug 02 '10 at 11:26
  • Thank you very much. It works perfectly. This should be in Net::SSH. – Dima Sabanin Apr 19 '11 at 13:27
  • 9
    Note... exit-signal returns a string so it should be exit_signal = data.read_string – pedz Oct 20 '12 at 14:21
  • 2
    I found this very useful in my scripts, so here is a gem wrapping this function: http://rubygems.org/gems/ssh-exec, source at https://github.com/mbautin/ssh-exec. – mikhail_b Feb 06 '14 at 07:19
  • I was unable to get exit_signal both with read_long and read_string. I tried to exit normal and to kill a sleeping process, it is always empty. Anyway, I don't need to get the signal, just curious. Excellent gem, thanks! – sekrett Apr 28 '16 at 10:49
7

Building on the answer by flitzwald - I've monkey patched my version of this into Net::SSH (Ruby 1.9+)

class Net::SSH::Connection::Session
  class CommandFailed < StandardError
  end

  class CommandExecutionFailed < StandardError
  end

  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
end
Mikey
  • 2,942
  • 33
  • 37
  • Did you consider sending this as a patch up-stream to them? – Lee Hambley Nov 19 '12 at 11:12
  • I'm currently working on a project which relies on this function - once I've got it tested I will. There are occasions where the exit code is > 0 despite successful execution, so I'll most likely modify the raise to be optional – Mikey Nov 20 '12 at 08:37
  • @Mikey What situations would that be? Would there be a nonzero exit code because the shell, ssh or library communication problem or would the command return nonzro exit code but did its job? – Phillipp Aug 20 '14 at 21:53
  • @Phillipp, I can't remember the circumstances, it was probably a specific tool which was not following convention. I think my main point was that I wanted the raise to be an optional. – Mikey Aug 21 '14 at 09:40
  • i'd add `channel.wait` to block on response (ruby 2+) – Hertzel Guinness Jul 27 '15 at 21:42
1

For newer versions of Net::SSH, you can just pass a status hash to Net::SSH::Connection::Session#exec:

status = {}

Net::SSH.start(hostname, user, options) do |ssh|
  channel = ssh.exec(command, status: status)
  channel.wait # wait for the command to actually be executed
end

puts status.inspect
# {:exit_code=>0}

By default, exec streams its output to $stdout and $stderr. You can pass a block to exec to do something different, a la:

ssh.exec(command, status: status) do |ch, stream, data|
  if stream == :stdout
    do_something_with_stdout(data)
  else
    do_something_with_stderr(data)
  end
end

This works on 6.1.0 - not sure about availability for older versions. See http://net-ssh.github.io/net-ssh/Net/SSH/Connection/Session.html#method-i-exec for more details.

Anthony Wang
  • 1,285
  • 1
  • 13
  • 14