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

how to report error when remote commands fail

here is the sample of my script iam trying to run on remote systems and execute commands

#!/usr/bin/perl -w use strict; use Net::SSH::Perl; my $hostname = "192.168.1.17"; my $username = "root"; my $password = "Pass1234"; my $cmd = 'ls'; my $cmd1 = 'ling -c 2 -q 8.8.8.8'; my $cmd2 = 'ping -w 3 8.8.8.8'; my @cmds = ($cmd , $cmd1, $cmd2); my $ssh = Net::SSH::Perl->new("$hostname", debug=>0); $ssh->login("$username","$password"); foreach my $one(@cmds) { my ($stdout,$stderr,$exit) = $ssh->cmd("$one") ; print $stdout; }

the stdout is outputting "bash: ling: command not found " for the $cmd1 but if the commands fail and then the script should warn me

here below is the output

suse@linux-p9uj:~/junk/vm> ./vmain.pl anaconda-ks.cfg bash: ling: command not found PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: icmp_seq=1 ttl=57 time=125 ms 64 bytes from 8.8.8.8: icmp_seq=2 ttl=57 time=52.1 ms 64 bytes from 8.8.8.8: icmp_seq=3 ttl=57 time=211 ms --- 8.8.8.8 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 52.113/129.706/211.260/65.033 ms

what iam trying to finally achieve is to run the script and see all the logs and errors in one place

Replies are listed 'Best First'.
Re: how to report error when remote commands fail
by hippo (Archbishop) on May 01, 2015 at 12:53 UTC

    Concatenate them something like this:

    my $out = ''; my $err = ''; foreach my $one(@cmds) { my ($stdout,$stderr,$exit) = $ssh->cmd($one) ; $out .= $stdout; $err .= $stderr; warn "'$one' has exit value $exit\n" if $exit; } print $out; # all the outputs concatenated print $err; # all the errors concatenated
Re: how to report error when remote commands fail
by GotToBTru (Prior) on May 01, 2015 at 14:26 UTC

    You will have to parse $stdout and $stderr to look for error/success indications. The cmd method is not going to raise a warning or abort if the remote command does, and I don't really think you want it to.

    Dum Spiro Spero