三年以前(抄)写的东西吧,今天要用,所以找了出来。
放在这儿备用。
#!/usr/bin/perl -w
use Net::FTP;
use strict;
my $server='xxx.xxx.xxx.xxx';
my $user='foo';
my $pw='bar';
my $rdir='/to';
my $ftp = Net::FTP->new($server);
$ftp->login($user,$pw) || die "Login failed";
$ftp->mkdir($rdir,1);
$ftp->cwd($rdir)|| die print "$rdir doesn't seem to exist on $server./n";
my $remote_start_dir = $ftp->pwd();
my $local_start_dir = '/from';
# my $local_start_dir = $ENV{PWD};
handle_dir($local_start_dir, $remote_start_dir);
sub handle_dir()
{
my $local=$_[0];
my $remote=$_[1];
opendir (DIR, $local) || die "huh? $!";
$ftp->mkdir($remote,1) || die "can't make $remote on $server/n";
my @subdirs;
my @all_files = grep !/^/./.?$/, readdir DIR;
foreach(@all_files)
{
if (-d $local . "/" . $_)
{
push @subdirs, $_;
}
else
{
$ftp->put($local . "/" . $_, $remote . "/" . $_)|| die "$!";
}
}
foreach (@subdirs)
{
handle_dir($local . "/" .$_, $remote . "/" . $_);
}
}
$ftp->quit;
本文展示了一段三年前编写的Perl代码,用于实现FTP服务器的登录和文件传输。代码中设置了服务器地址、用户名和密码,登录服务器后创建目录,然后递归处理本地目录,将文件上传到服务器指定目录,最后退出FTP连接。

249

被折叠的 条评论
为什么被折叠?



