Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1458089
  • 博文数量: 408
  • 博客积分: 10036
  • 博客等级: 上将
  • 技术积分: 4440
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-06 13:57
文章分类

全部博文(408)

文章存档

2011年(1)

2010年(2)

2009年(1)

2008年(3)

2007年(7)

2006年(394)

我的朋友

分类:

2006-07-24 17:51:33

perl程序——获得ftp文件列表
 
#!/usr/bin/perl

use strict;
use Net::FTP;
use File::Path;
use Getopt::Long;

die unless my($ID,$USERNAME,$PASS,$HOST,$P,$PORT)=$ARGV[0]=~/ftp:\/\/((.+):(.+)@)?([^:]+)(:(.+))?/;

my $ftp=Net::FTP->new($HOST,Port=>$PORT,Passive=>1) or die "Could not connect: $@\n";
$ftp->login($USERNAME,$PASS) or die "Could not login:",$ftp->message;
print "Login successfully\n";

get_tree("/");

$ftp->quit;
exit 0;

sub get_tree {
my $path=shift;
my $curr=$ftp->pwd;
if(!$curr) {
print "Fatal error: function pwd returned NULL.\n";
print "Message from server: ".$ftp->message."\n";
print "Current dir is: ".$ftp->pwd."\n";
print "Message from server: ".$ftp->message."\n";
exit -1;
}

#This is important.Because $ftp->pwd($DIR) will not work if $DIR is space(s)
my $s=$ftp->_CWD($path);

if(!$s) {
print "Can not change current directory to ".$path."\n";
return;
}

print $ftp->pwd."\n";

foreach($ftp->dir) {
next unless my($type,$name)=&parse_listing($_);
next if $name=~/(\.|\.\.)$/;

if($type eq 'd') {
&get_tree($name);
}
elsif ($type eq '-') {
print $ftp->pwd."/";
print "$name\t";
print $ftp->size($name);
print "\n";
}
}
if(!$ftp->cwd($curr)) {
print "Fatal error: could not change to $curr.\n";
print "Message from server: ".$ftp->message."\n";
print "current dir is: ".$ftp->pwd."\n";
print "Message from server: ".$ftp->message."\n";
exit -1;
}
}

sub parse_listing {
my $list=shift;
return unless my ($type,$mode,$name)=
$list=~/^([a-z-])([a-z-]{9})
\s+\d*
(?:\s+\w+){2}
\s+\d+
\s+\w+\s+\d+\s+[\d:]+
\s(.+)
$/x;
return ($type,$name);
}
================
注: 许多ftp服务器支持 ls -R 命令
阅读(1171) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~