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

Hi Monks,

First off I'm new to this site so please be kind :-)

I have wrote a script which will be loaded on to several remote machines. The script executes Linux commands using backticks and sets an exit status depending upon how the script progressed. Unfortunately I have to use the backtick method to return the output of the executed command so that I can distinguish between a truely successful outcome and one where a 0 exit code is returned but the outcome is not actually a success. This means I can't use '$?' to check for a correct exit status.

###remote.script### $result1=`DB_COMPILE $from $to`; if ($result1 =~ m/0 errors, 0 warnings/) { <<SUCCESS - SOME CODE>> exit_status("Success\n","0"); }else { exit_status("DB_COMPILE failed\n","1"); } sub exit_status { my $description=shift; my $status=shift; print "$description\n"; exit("$status"); }
As this script exists on multiple remote machines, I tried to code another script to launch the remote code and return the exit status as set in 'remote.script' using Net::SSH:Perl. The problem is that Net::SSH::Perl also returns stdout and appears to "grab" stdout from the remote script. This has the effect that the remote script can not use the output of the Linux executed commands for error checking as stdout is directed to 'local.script'.
###local.script### %codes=('0' => 'Success', '1' => 'General failure',); my $ssh = Net::SSH::Perl->new("$ip"); $ssh->login ($user, $pass); ($exit) = $ssh->cmd("Remote.script"); $description=$codes{"$exit"}; print "EXIT STATUS: $exit $description\n";
Is there a way to turn off Net::SSH::Perl's ability to grab stdout from the remote session whilst still enabling me to grab the exit status? I have read the docs on CPAN, confused myself no end and then googled this problem, but still can't find an answer. Normally this excellent Perl module would be a godsend, but just not in this instance where I need stdout to stay on the remote machine.

Replies are listed 'Best First'.
Re: Turn off stdout in Net::SSH::Perl
by Anonymous Monk on Sep 02, 2008 at 12:34 UTC
    This has the effect that the remote script can not use the output of the Linux executed commands for error checking as stdout is directed to 'local.script'.
    Not likely. Does the same happen if you use a non-perl ssh client?
      Anonymous Monk, thanks for your input. It got me thinking and I created a shell script with a one liner in it:
      output=`ssh root@172.17.194.60 /u/remote.script`;
      This also failed, but several print statements in remote.script showed that my back ticked line was not being executed at all! I then created a simple perl one-liner on the remote machine and noted that this worked if I ran it remotely from my shell script.
      $result=`/bin/ls -l /u/tns/`;
      Then the answer struck me...my remote.script did not have the full path in front of DB_COMPILE. Once this was inserted everything worked perfectly!
      $result1=`/u/DB_COMPILE $from $to`;
      This has been bugging me for ages, so thanks for making me think again and setting me off in the right direction!