nt2282 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying to write a perl program to get the yesterday files from a ftp site. The program is working fine but i wanted to do some modifications in that and needed some help regarding that. The structure of the directories are -- Some directory say a/b/c/d inside that there are multiple folders like t1, t2,t3 and t4 . All the tridents directory contains hourly log files of everyday. My script is taking yesterday log files. I am facing two problems- 1. The script gets time out after an hour, i wanted it not to time out. 2. The logs get updated houly so the script remains in one directory itself say t1, it does not go to the other directory, i wanted to download parrelelly if possible or any other ways to download files from all directory. The script is --
#!/usr/bin/perl use Net::FTP; use Archive::Zip; use IO::File; use File::Spec; use File::Copy; print "Retrieving file from logs.com...\n"; $loginip = '1.2.3.4'; $loginid = 'loginid'; $loginpaswd = 'pswd'; ( $sec, $min, $hour, $mday, $mon, $year ) = ( localtime(time) )[ 0, 1, 2, 3, 4, 5 ]; $time_stamp = "_" . ( 1900 + $year ) . "_" . ( $mon + 1 ) . "_" . ($ +mday) . "_" . $hour . "_" . $min . "_" . $sec; printf "time stamp = $time_stamp\n"; unless ( -d "SEC5TFLogs" ) { mkdir("SEC5TFLogs"); } if ( -d "SEC5TFLogs" ) { chdir("SEC5TFLogs"); } $ftp = Net::FTP->new( ($loginip), Debug => 0 ) or die "Cannot connect to logs.com: $@ \n"; $ftp->login( $loginid, $loginpaswd ) or die "Cannot login ", $ftp->message; $source_dir = "/a/b/c/d/"; print "Source Dir is $source_dir\n"; my @dir= $ftp->ls($source_dir); foreach $trident (@dir) { print "The tridents are $trident\n"; $ftp->cwd($trident) or die "Cannot change working directory ", $ftp-> +message; $ftp->binary || die "Unable to set mode to binary. ", $ftp->message; my $destinationDirectory = '\\\\1.15.1.40\E$\Sec5Logs'; my($vol,$dir,$tridentfile) = File::Spec->splitpath($trident); print "$tridentfile\n"; mkdir "$destinationDirectory/$tridentfile"; @list = $ftp->ls(); my ( $var1, $var2, $var3, $var4, $var5 ) = split( / /, scalar( localti +me( time - 86400 ) ) ); my $dateformat = "$var2$var3$var5"; print "Dateformat is $dateformat\n"; #printf "list = \n"; #print @list; foreach $file (@list) { if ( $file =~ /Trident..*$dateformat..*\.zip/i ) { $ftp->get($file) or die "get failed ", $ftp->message; printf "ftp done\n"; printf "Moving the file to 1.15.1.40 machine\n"; move("$file","$destinationDirectory/$tridentfile/$file"); printf "Moving of the file $file is done\n"; } print "The files are downloaded successfully\n"; closedir DIR; } $ftp->quit;

Replies are listed 'Best First'.
Re: FTP to local machine
by raybies (Chaplain) on Mar 17, 2011 at 16:58 UTC
    so are you saying it takes an hour to ftp something? Or do you try to keep the connection open that long? And Why not reopen the connection, each time you want to get something? Otherwise you have to keep the connection open, by periodically "doing something" (like an ls?), which is a waste of process/network time... when you could just sleep and reopen it...