I'm approaching the world of perl these days.
I created a script that would automatically download from an FTP new files modified in the last 24 hours and with .tar extension, but unfortunately the script fails.
This is the script
use strict;
use warnings;
use Net::FTP;
use DateTime ();
use DateTime::Duration ();
use File::Listing qw(parse_dir);
use POSIX qw(strftime);
use File::stat;
my $pathto = "/mnt/tmp";
my $logname = "_log";
my ($host, $user, $passwd) = ('IP.ADD.RE.SS', 'user', 'password');
my $dir = '/common';
my $timesplit = 24;
my $extension = '.tar';
my @noseries = ('monkey','dog','rubbit');
sub findstring
{
foreach my $series(@noseries) {
if (index((lc $_[0]),(lc $series)) != -1) {
return 1;
}
}
return 0;
}
sub printclose
{
print MYOUTFILE localtime()."\: $_[0]\n";
close(MYOUTFILE);
return "$_[0]\n";
}
sub printfile
{
print MYOUTFILE localtime()."\: $_[0]\n";
return "$_[0]\n";
}
my $now = DateTime->now;
my $delta = DateTime::Duration->new( hours => $timesplit );
my $cutoff = $now->subtract_duration( $delta );
open(MYOUTFILE, '>>'.$pathto.$logname)
or die "Impossibile creare o aprire il file di log"; #open for wri
+te, append
print printfile('Avvio operazione');
print printfile('Mi connetto al server');
my $ftp = Net::FTP->new($host, Debug=> 1, Passive => 1 )
or die printclose("Non riesco a connettermi a $host");
print printfile('Effettuo il login');
$ftp->login($user, $passwd)
or die printclose('Cannot login');
$ftp->binary();
print printfile('Seleziono la directory');
$ftp->cwd($dir)
or die printclose('Cannot cwd to $dir');
print printfile('Cerco file da scaricare');
my $ls = $ftp->dir()
or die printclose('Cannot list $dir');
foreach my $entry (parse_dir($ls)) {
my ($name, $type, $size, $mtime, $mode) = @$entry;
next unless ($type eq 'f' && ((lc (substr $name, -4)) cmp $extensi
+on) == 0 );
my $tz = DateTime::TimeZone->new( name => 'local' );
( my $sec,my $min,my $hour,my $mday,my $months,my $year,my $wday,m
+y $yday,my $isdst) = localtime($mtime);
$year= $year+1900;
$months=$months+1;
my $dt = DateTime->new(year => $year, month => $months, day => $m
+day, hour => $hour, minute => $min, second => $min, time_zone => Date
+Time::TimeZone::Local->TimeZone());
my $cmp = DateTime->compare( $dt, $cutoff );
if ($cmp >=0) {
if (findstring($name)==0) {
print (printfile("Downloading $name"));
$ftp->get($name,"$pathto$name")||
die printclose("get($name) failed: ". $ftp->code() . ": "
+. $ftp->message());
my $filesize = stat("$pathto$name")->size;
if ($filesize!=$size) {
print (printfile("Attenzione: File scaricato ma il siz
+e non corrisponde, possibile errore nel download"));
}
else {
print (printfile("Scaricato con successo"));
}
}
}
}
$ftp->quit;
print printclose('Operazione completata con successo');
While this is the debug
root@pc2:~# perl filenew.pl
Avvio operazione
Mi connetto al server
Net::FTP>>> Net::FTP(2.77)
Net::FTP>>> Exporter(5.67)
Net::FTP>>> Net::Cmd(2.29)
Net::FTP>>> IO::Socket::INET(1.33)
Net::FTP>>> IO::Socket(1.34)
Net::FTP>>> IO::Handle(1.33)
Net::FTP=GLOB(0x30ef478)<<< 220-FileZilla Server version 0.9.41 beta
Net::FTP=GLOB(0x30ef478)<<< 220-written by Tim Kosse (Tim.Kosse@gmx.de
+)
Net::FTP=GLOB(0x30ef478)<<< 220 Please visit http://sourceforge.net/pr
+ojects/filezilla/
Effettuo il login
Net::FTP=GLOB(0x30ef478)>>> USER user
Net::FTP=GLOB(0x30ef478)<<< 331 Password required for user
Net::FTP=GLOB(0x30ef478)>>> PASS ....
Net::FTP=GLOB(0x30ef478)<<< 230 Logged on
Net::FTP=GLOB(0x30ef478)>>> TYPE I
Net::FTP=GLOB(0x30ef478)<<< 200 Type set to I
Seleziono la directory
Net::FTP=GLOB(0x30ef478)>>> CWD /common
Net::FTP=GLOB(0x30ef478)<<< 250 CWD successful. "/common" is currentdi
+rectory.
Cerco file da scaricare
Net::FTP=GLOB(0x30ef478)>>> PASV
Net::FTP=GLOB(0x30ef478)<<< 227 Entering Passive Mode (IP.ADD.RE.SS,82
+,8)
Net::FTP=GLOB(0x30ef478)>>> LIST
Net::FTP=GLOB(0x30ef478)<<< 150 Connection accepted
Net::FTP=GLOB(0x30ef478)<<< 226 Transfer OK
Downloading mainupdate_20130218.tar
Net::FTP=GLOB(0x30ef478)>>> PASV
Net::FTP=GLOB(0x30ef478)<<< 227 Entering Passive Mode (IP.ADD.RE.SS,82
+,9)
Net::FTP=GLOB(0x30ef478)>>> RETR mainupdate_20130218.tar
Net::FTP=GLOB(0x30ef478)<<< 150 Connection accepted
Net::FTP=GLOB(0x30ef478): Timeout at /usr/share/perl/5.10/Net/FTP/data
+conn.pm line 74.
Unable to close datastream at /root/filenew.pl line 73.
get(mainupdate_20130218.tar) failed: 150: Connection accepted
Thanks,
Logan