Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > net::openssh - how to get process exit status?

Reply
Thread Tools

net::openssh - how to get process exit status?

 
 
Ryan Grow
Guest
Posts: n/a
 
      09-26-2008
Hi,

I am using net-ssh 2.0.1. I have not been able to figure out how to get
the exit value from a remotely executed process. In the only examples I
could find, only the stdout was returned, like this one:


Net::SSH.start(host, username, assword => password) do |ssh|
print ssh.exec!("hostname")
end

I would like to check to see if the remote command returns a non-zero
value to indicate failure.

Thanks,

Ryan
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Young Hyun
Guest
Posts: n/a
 
      09-29-2008
On Sep 26, 2008, at 1:46 PM, Ryan Grow wrote:

> I am using net-ssh 2.0.1. I have not been able to figure out how to
> get
> the exit value from a remotely executed process. In the only
> examples I
> could find, only the stdout was returned, like this one:
>
>
> Net::SSH.start(host, username, assword => password) do |ssh|
> print ssh.exec!("hostname")
> end


If you want the process exit status, you have to forgo the ssh.exec!()
convenience method and work at a lower level. Here's how to do it:

ssh.open_channel do |channel|
channel.exec(command) do |ch, success|
unless success
abort "FAILED: couldn't execute command (ssh.channel.exec
failure)"
end

channel.on_data do |ch, data| # stdout
print data
end

channel.on_extended_data do |ch, type, data|
next unless type == 1 # only handle stderr
$stderr.print data
end

channel.on_request("exit-status") do |ch, data|
exit_code = data.read_long
if exit_code > 0
puts "ERROR: exit code #{exit_code}"
else
puts "success"
end
end

channel.on_request("exit-signal") do |ch, data|
puts "SIGNAL: #{data.read_long}"
end
end
end
ssh.loop

--Young


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Spawn a process, then exit, whilst leaving process running? Victor Hooi Python 1 02-10-2013 03:50 AM
I don't get why sys.exit(1) doesn't exit the while loop in the follow case chad Python 14 10-13-2010 09:08 AM
Can I get the exit code "n" passed to sys.exit(n) ? Yujo Python 2 04-10-2007 08:35 PM
exit after process exit ajikoe@gmail.com Python 2 05-31-2005 08:11 PM



Advertisments