- 作者:zhaozj
- 发表时间:2020-12-23 10:56
- 来源:未知
三年以前(抄)写的东西吧,今天要用,所以找了出来。放在这儿备用。
#!/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;