I am wanting to test to make sure that an ftp transfer in fact takes
place. Searching through newsgroups, saw several recommendations re: Perl
Cookbook's 16.21, which I have used below. I think I have done this
correctly -- code below -- but will this be enough? 

I am assuming that if the script times out, unable to log on for 3600
seconds, it will send me the alternative email that about the timeout.

But what if this script just runs every day and I forget about it and the
host changes the username and doesn't notify me or change the dir
structure? I notice if I change the username or filename or password to
something that I know is wrong, the script says:

Uncaught exception from user code
eval {...} called

But how to I catch that and send an email warning me of these problems,
too?

Am I making sense? Any suggestions much appreciated.

Gary


#!/usr/bin/perl -w
 
use diagnostics;
use Net::FTP; 

$dir = "/home/dir/"; 
$site = 'ftp.address'; 
$username = 'username'; 
$password = 'password';
$file = 'filename'; 
$unzipped = 'another.file';

    chdir("$dir");
    if (-e $unzipped) 
        { unlink($unzipped); print "$unzipped removed.\n"; } 
        else { print "No $file exists.\n"; }

## wrap in eval perl cookbook p. 16.21

$SIG{ALRM} = sub { die "timeout" };

eval {
    alarm(3600);

## long-time operations here:

    $ftp = Net::FTP->new($site,
                     Timeout => 60,
                     Passive => 1,
                     Debug => 3)
    or die "Can't connect: $@\n";
    $ftp->login($username,$password)
    or die "Couldn't authenticate.\n";
    $ftp->cwd("log/");
    $ftp->binary;
    $ftp->get ($file) or die "Something is wrong here getting $file\n";
    $ftp->quit;
    system "/bin/gunzip $unzipped";         

# email recipient of success

open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
        or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<"EOF";
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: Headline

The latest is available...
EOF
close(SENDMAIL) or warn "sendmail didn't close nicely";



## continue with eval

    alarm(0);

};

if ($0) {
   if ($0 =~ /timeout/) {
                                
# timed out; do what you will here
# notify webmaster of problem



open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
        or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<"EOF";
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Subject: Timeout problem 

... timed out.
EOF
close(SENDMAIL) or warn "sendmail didn't close nicely";


} else {
    alarm(0);
    die;
    }
}

## end eval



_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to