老玉米:
用perl谢,1-20分钟的事情...帖一个我刚才写的通用文件传输的tool..(还没有完成的)
[阅读: 571] 2006-06-23 08:24:03
use Net::FTP;
use File::Path;
use Switch;
sub print_uage()
{
print "Parameters error!\n";
print "Usage:\n";
die "perl ftp.pl <put|get> -h host_name -u username -p password -l local_dir -r remotedir [FileName_RegEx]\n";
}
if ($#ARGV < 10)
{
print_uage;
}
#for(my $index =0; $index <= $#ARGV ;$index++){print "$ARGV[$index]\n";}
my $ftp_put;
if ( $ARGV[$0] eq "put" ){
$ftp_put = 1;
}
else {
if ( $ARGV[$0] eq "get" ){
$ftp_put = 0;
}
else{
print_uage;
}
}
my $index;
my $server;
my $user;
my $password;
my $locoldir;
my $remotedir;
my $RegEx;
$/ = "\\";
#for( $index = 0; $index <= $#ARGV; $index++){print "$ARGV[$index]\n";}
$index = 1;
while ( $index <= $#ARGV ){
switch ($ARGV[$index]) {
case "-h" { $server = $ARGV[$index+1];$index++; }
case "-u" { $user = $ARGV[$index+1];$index++; }
case "-p" { $password = $ARGV[$index+1];$index++; }
case "-l" { $locoldir = $ARGV[$index+1];$index++; }
case "-r" { $remotedir = $ARGV[$index+1];$index++; }
}
$index++;
}
if ( $#ARGV == 11 )
{
$RegEx = $ARGV[11];
}
#print "server: $server\n";
#print "user: $user\n";
#print "password: $password\n";
#print "locoldir: $locoldir\n";
#print "remotedir: $remotedir\n";
#print "RegEx: $RegEx\n";
$ftpobj = Net::FTP -> new ($server);
$ftpobj -> login($user,$password) or die "Cannot login ftp $server !!!";
print "Connecting to $server successfully!\n";
$ftpobj -> cwd ("$remotedir") or die "Cannot change to directory $remotedir !!!";
print "Change to $remotedir!\n";
if ( $ftp_put == 1 ){
}
else{
@files = $ftpobj -> dir();
if ( $#files == -1 ){
$ftpobj -> quit;
die "Cannot find any files!\n";
}
for($index =0; $index <= $#files ;$index++){
$_ = $files[$index];
#print "$_\n";
if ( /^-/ ){
if ( /([^ ]+)$/ ){
my $localfile = "$locoldir$/$1";
$ftpobj -> get ($1,$localfile) or die "Canot get file $localfile !!!";
print "file '$localfile' size ",-s $localfile," bytes\n";
}
}
}
}
$ftpobj -> quit;
exit 0;