I found this very useful perl script that will check a remote ftp server, search for files of a specific time and get them. When I run the script it works, but it gave me the following error:
Couldn't get filename_12-13-07.txt Bad file number
What in this script would cause this? I know nothing about perl.
#!/usr/bin/perl
use Net::FTP;
$date=shift @ARGV;
@months=qw(null Jan Feb Mar Apr My Jun Jul Aug Sep Oct Nov Dec);
@hosts=qw(ftp.server.com);
@dirs=qw(/);
@logins=qw(username);
@passwords=qw(password);
$x=0;
foreach(@months) {
$months{$_}=$x++;
}
# we need this hash later
if (not $date) {
$now=time();
$now -= (24 * 3600 );
# yesterday
($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
$nowyr+=1900;$nowmon++;
$date=sprintf("%.2d/%.2d/%.4d",$nowmon,$nowday,$nowyr);
print "Using $date\n";
}
$now=time();
($nowsec,$nowmin,$nowhr,$nowday,$nowmon,$nowyr,$nowdow,$nowdoy,$nowdst)=localtime($now);
$nowyr+=1900;
# need $nowyr later
($month,$day,$year)=split /\//,$date;
#
# broken next century - blame me then
#
$year+=2000 if $year < 100;
$x=0;
foreach (@hosts) {
$newerr=0;
$ftp=Net::FTP->new($,Timeout=>240) or $newerr=1;
push @ERRORS, "Can't ftp to $: $!\n" if $newerr;
next if $newerr;
print "Connected $_\n";
$ftp->login($logins[$x],$passwords[$x]) or $newerr=1;
push @ERRORS, "Can't login to $: $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Logged in $\n";
$ftp->cwd($dirs[$x]) or $newerr=1;
push @ERRORS, "Can't cd $dirs[$x] on $_ $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Getting file list $_\n";
@files=$ftp->dir or $newerr=1;
push @ERRORS, "Can't get file list on $_ $!\n" if $newerr;
$ftp->quit if $newerr;
next if $newerr;
print "Got list $_\n";
print "Looking for $date $time\n";
foreach(@files) {
$_=substr($_,41);
s/ */ /g;
s/^ *//g;
chomp;
@stuff=split / /;
# if it's today, the year slot will have time instead
# so make it this year
$stuff[2]=$nowyr if /:/;
$ftp->quit if \($stuff[2] < $year\);
next if \($stuff[2] < $year\);
$ftp->quit if \($months\{$stuff[0]\} < $month and $stuff[2] == $year\);
next if \($months\{$stuff[0]\} < $month and $stuff[2] == $year\);
$ftp->quit if \($stuff[0] < $day and $stuff[2] == $year and $months\{$stuff[0]\} == $month\);
next if \($stuff[1] < $day and $stuff[2] == $year and $months\{$stuff[0]\} == $month\);
print "Getting $_\\n";
$ftp->get\($stuff[3],$stuff[3]\) or $newerr=1;
push @ERRORS, "Couldn't get $stuff[3] $!\\n" if $newerr;
}
$ftp->quit;
}
print @ERRORS;
exit 0;