sdolancu has asked for the wisdom of the Perl Monks concerning the following question:

When the script below executes the command, it does so on another host by ssh'ing (or rsh'ing) to that box. I need to capture the return code of the script. I have tried using pipes but I always get a successful $? because the ssh/rsh is always successful. Let me show you:
---- test.sh --- #!/bin/sh echo "Hello world" exit 1 <-- Note that we are forcibly exiting non-zero ---- runme.pl ---- #!/usr/bin/perl local $SIG{CHLD} = sub { wait; } # Shell to the "hosta" and run the script. # Since it will fail, return the word FAIL my $cmd = "ssh hosta -n \"/tmp/test.sh || echo FAIL\"; my ($read, $write, $pid) = spawnMe($cmd); close ( $write ); # Not doing any writes while ( <$read> ) { my $buf = 'X' x 512; if ( my $nread = sysread( $read, $buf, 512 ) ) { my @l = split /\r?\n/, $buffer . $buf; if ($buf !~ /\n$/s ) { $buffer = pop @l; } else { $buffer = ''; } map { print "$_\n"; } @l; } } my $kid = waitpid($pid, WNOHANG); unless ($kid == $pid) { close ($read); $return_code = $?; } else { print "Reaped status: $?" $return_code = $?; }
# I always get an exit status of "0" because the ssh was successful. How do I get the "FAIL" ? Help.

Replies are listed 'Best First'.
Re: Pipe and return status through an "ssh"
by pc88mxer (Vicar) on Jun 10, 2008 at 18:21 UTC
    You are kinda already testing the exit status of your command by using || echo FAIL in your shell command. How about just tacking on the exit status in the output:
    my $cmd = q{ssh hosta -n "/tmp/test.sh; echo EXIT STATUS: $?"};
    Then just look for EXIT STATUS: ... in your captured output.

    Alternatively, look at Net::SSH::Perl. The cmd method will return the output, error output and exit status of a remotely executed command.

Re: Pipe and return status through an "ssh"
by Crackers2 (Parson) on Jun 10, 2008 at 18:54 UTC

    As the previous reply hints at, it has nothing to do with the ssh and everything with the ||. You can verify that it behaves the same way on a shell command line:

    :/tmp/t # false; echo $? 1 :/tmp/t # false || echo FAIL; echo $? FAIL 0

    So either just remove the || echo FAIL part, or make it something like || { echo FAIL; exit 1 }