中国开发网: 论坛: 程序员情感CBD: 贴子 348134
老玉米: 用perl谢,1-20分钟的事情...帖一个我刚才写的通用文件传输的tool..(还没有完成的)
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;
民主不同于专制,不需要强加于人。--- 賴斯

大道之行也,天下为公,选贤与能,讲信修睦。故人不独亲其亲,不独子其子,使老有所终,壮有所用,幼有所长,矜、寡、孤、独、废疾者,皆有所养。男有分,女有归。货恶其弃於地也,不必藏於己;力恶其不出於身也,不必为己。是故,谋闭而不兴,盗窃乱贼而不作,故外户而不闭,是谓大同。--《礼运·大同篇》

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录