连接ftp有两种连接模式,默认模式和passive模式。
ftp服务器工作在默认模式下,连接比较简单:
use Net::FTP;
#initial ftp
my $ftp = Net::FTP->new($ftpserver,Debug => 0, Timeout => 60)
or return -1;
$ftp->login($ftpUserName, $ftpPassword)
or return -1;
#get file
$ftp->binary or die "Cannot change binary mode ", $ftp->message;
#cd remote
$ftp->cwd($remotedir) if($remotedir ne '');
my @files = $ftp->ls('*'.$lasthour.'*') or return -1;#获取文件列表
foreach my $file (@files)
{
$ftp->get($file,$localdir.'/'.$file) or die "get failed ", $ftp->message;
}
$ftp->quit;
在passive模式下,之需要打开一个参数即可:
$ENV{FTP_PASSIVE} = 1; #passive mode
这样就可顺利建立连接了~