#!/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 命令