我的要求是将文件和文件夹从 SFTP 远程位置传输到 unix 位置。我是 perl 和 shell 脚本的新手,这非常令人困惑。我尝试使用来自 sftp foreign 的 rget 命令,如下所示。但它不起作用,也没有被转移。从 sftp 服务器复制文件和文件夹后,如何删除它们?
use Net::SFTP::Foreign;
my $host = "sftp.elsevier.com";
my $remote_dir = "sample/host";
my $localdir = "sample/local";
my $username = "username";
my $password = "password";
my $debug = 0;
$sftp = Net::SFTP::Foreign->new($host, timeout => 240,user => $username, password => $password) or $newerr = 1;
push @ERRORS, "Can't sftp to $host: $!\n" if $newerr;
myerr() if $newerr;
print "Connected to $host\n";
$sftp->rget($remote_dir, $localdir) or $newerr = 1;
push @ERRORS, "Cant tranfer\n" if $newerr;
myerr() if $newerr;
$sftp->disconnect if $newerr;
print "Files are transferred successfully\n";
#Deleting files
$sftp->setcwd($remote_dir) or $newerr = 1;
push @ERRORS, "Can't cd $!\n" if $newerr;
myerr() if $newerr;
$sftp->disconnect if $newerr;
my $files = $sftp->ls or $newerr = 1;
push @ERRORS, "Can't get file list $!\n" if $newerr;
myerr() if $newerr;
foreach (@$files){
$sftp->remove($_) or $newerr = 1;
push @ERRORS, "Cant delete\n" if $newerr;
myerr() if $newerr;
print "Deleted files successfully\n";
#}
$sftp->disconnect;
sub myerr {
print "Error: \n";
print @ERRORS;
exit 0;
}
在 SFTP 位置 - sample/host 内,我有许多文件夹和文件都需要复制到本地 unix 服务器 - sample/local 并从远程位置删除。