#!/usr/bin/perl # # gets $FILE in $DIR from $HOST # # the following command line parameters are accepted: # # -h host IP and port (ex: 192.168.1.1 22) # -u userid (ex: -u anonymous) # -p password (ex: -p your@email.com) # -d directory (ex: -d /pub/data/) # -f file (ex: -f filename.txt) # NOTE: if -f is not passed, then the entire directory is downloaded $BadInput = ""; use Getopt::Long; GetOptions("h=s" => \$HOST, "port=s" => \$PORT, "u=s" => \$USERNAME, "p=s" => \$PASSWORD, "d=s" => \$DIR, "f=s" => \$FILE); use File::stat; $fileinfo = stat("$ENV{HOME}.batchftprc"); if (!($HOST)) { print "No host specified\n"; $BadInput = "Y"; } if (!($USERNAME)) { print "No userid specified\n"; $BadInput = "Y"; } if (!($PASSWORD)) { print "No password specified\n"; } if ((!($DIR)) and (!(-s("$ENV{HOME}/.batchftprc")))) { print "No directory specified\n"; $BadInput = "Y"; } if ($BadInput) { print "correct usage is: perl batchftp.pl --h= [--port=] --u= --p= --d= [--f=]\n"; print "items enclosed in [] are optional\n"; print "-d is required unless $ENV{HOME}/.batchftprc exists\n"; print "probably want to use & as well\n"; exit; } if ($DIR) { push(@dir, $DIR); } else { open(CONFIG, "$ENV{HOME}/.batchftprc") or die "-d not specified and could not open $ENV{HOME}/.batchftprc: $!"; while (chomp($DIR = )) { push(@dir, $DIR); } } use Net::FTP; $ftp = Net::FTP->new("$HOST $PORT") or die "can't connect to $HOST: $!\n"; $ftp->login($USERNAME, $PASSWORD) or die "can't login: $!\n"; foreach $DIR (@dir) { $ftp->cwd($DIR) or die "can't change to $DIR: $!\n"; if ($FILE) { $ftp->get($FILE) or die "can't get $FILE: $!\n"; } else { @list = $ftp->ls or die "can't get ls: $!\n"; foreach $FILE (@list) { $ftp->get($FILE) or die "in foreach can't get $FILE: $!\n"; } } } $ftp->quit; #### /directory01/whatever /directory02 /directory03/whatever/you/want