I think this is what you are looking for. I didnt get the chance to test it (sorry, Im way too tired and my eyes are crawling shut). Please share your code with the rest of us when you get it working!
btw, You really should add error checking for the ftp transactions!
#!/usr/bin/perl -w
use Strict;
use Net::FTP;
my $host = 'www.ftp.com';
my $user = 'anonymous';
my $pass = 'updatepuller@ftp.com';
my $remote_dir = '/pub/antivirus/NAV/signatures';
my $destination_dir = 'current-sig';
my $UPDATE_TRACKER = './lastupdate.txt';
# You should add error checking to make sure you actually make
# a connection to the server
my $ftp = Net::FTP->new($host, Debug => 1);
$ftp->login($user,$pass);
my @listing = $ftp->ls($remote_dir);
foreach (@listing) {
$_=~s/\r|\n//g;
if ( /x86\.exe/gi ) {
my ($response,$errmsg) = do_we_already_have_it($_);
if ( $response != 1 ) {
# Either we have it or we have an error
if ($response == -1) {
# We have an error
print "Error - $errmsg\n";
quit_now();
};
} else {
# We dont have it - grab it
($response,$errmsg) = grab_it($_);
if ( $response == -1 ) {
# We have an error
print "Error - $errmsg\n";
quit_now();
};
};
};
};
quit_now();
sub quit_now {
$ftp->quit;
exit;
};
sub grab_it {
# returns 1 if we get the file
# returns -1 if we dont get the file
# in its current state it will only return -1 if we cant update
# the tracker db
my ($remote_file) = @_;
$ftp->type("I");
$ftp->get($remote_file);
# You should add error checking here to make sure you actually
# get the file !! ( I would but im too tired )
if ( open(NEWUPDATE,">>$UPDATE_TRACKER") ) {
print NEWUPDATE "$remote_file\n";
close(NEWUPDATE);
return(1);
} else {
# Something went wrong return -1 and the error msg
return(-1,$!);
};
};
sub do_we_already_have_it {
# returns 1 if we already have it
# returns 0 if we dont have it
# returns -1,error msg if something goes wrong (like permissions)
my ($check_file) = @_;
$check_file=~s/\r|\n//g;
if ( open(LASTUPDATE,$UPDATE_TRACKER) ) {
while(<LASTUPDATE>) {
$_=~s/\r|\n//g;
if ($_ eq $check_file) {
close(LASTUPDATE);
return(1);
};
};
close(LASTUPDATE);
return(0);
} else {
# something went wrong - check if the file exists
# If the file exists then we have some sort of other error
# that we should probably report back to the client
# otherwise we simply dont have it yet !
if(-e $UPDATE_TRACKER) { return(-1,$!) };
return(0);
};
};