連接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
這樣就可順利建立連接了~