Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

help with scp error codes

by wskibum (Initiate)
on Mar 16, 2008 at 07:48 UTC ( #674443=perlquestion: print w/ replies, xml ) Need Help??
wskibum has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I am a newbie at Perl, nuf said there I guess.

I have a need to move my log files around and I am using a simple perl scp script to do this. Now I need to add some error reporting to the script and I am in over my head.

1: I need to send an email when scp cannot connect to a server
2: I need to send and email if scp cannot find the file to download How can I get the error codes on a failure and what do I do with it?

Please help me out if you can, I've been searching the web with no luck for 2 days.

currently my scp command looks something like this:

GET THE FILE WITH SCP # $filetoget="$LOGFILEDIR/$f\.@FILEDOMAIN\$i\.$year-$mon-$mday"; $command = "/usr/bin/scp $a:$filetoget $f\.@FILEDOMAIN\$i\.$year$mon$m +day\.$a";

Comment on help with scp error codes
Download Code
Re: help with scp error codes
by ikegami (Pope) on Mar 16, 2008 at 08:18 UTC

    On my system, scp return an error code on error. ("DIAGNOSTICS: scp exits with 0 on success or >0 if an error occurred.") system returns and sets $? to a value that contains the exit code of the process it launched.

    my $filetoget = "$f.\@FILEDOMAIN$i.$year-$mon-$mday"; my @cmd = ( '/usr/bin/scp', "$a:$LOGFILEDIR/$filetoget", "$filetoget.$a", ); my $rv = system(@cmd); if ($rv == -1) { print("Unable to execute scp: $!\n"); } elsif ($rv & 127) { printf("scp died with signal %d\n", ($rv & 127)); } elseif {$rv >> 8) { printf("scp exited with error %d\n", ($rv >> 8)); } else { print("scp was successful\n"); }

    As for the email part, MIME::Lite should do the trick.

    Update: Oops, was missing scp's 2nd arg.

      MIME::Lite is a fine (and more reliable) option, but seeing we are just shelling out to scp why not just invoke sendmail directly by opening a pipe and sending it the required data:

      my $sendmail = "/usr/sbin/sendmail -t"; open MAIL, "|$sendmail" or die "Cannot open $sendmail: $!"; print MAIL <<EMAIL; Reply-to: $reply_to Subject: $subject To: $send_to Content-type: text/plain $content . EMAIL close MAIL;
Re: help with scp error codes
by apl (Monsignor) on Mar 16, 2008 at 13:35 UTC
    Another way of e-mailing to user@host.com is by calling:
    sub Mail { my ( $Msg ) = @_; my $Hdr = "From: Speaker to Users\n". "To: <user\@host.com>\n". "Subject: End of Day problems\n\n"; open( SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") or die( "Can't fork for sendmail: $! \n" ); print SENDMAIL $Hdr.$Msg; close(SENDMAIL) or warn "sendmail didn't close correctly!"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://674443]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (19)
As of 2014-10-30 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls