我正在尝试使用 Perl Net::SFTP::Foreign
模块将文件从远程服务器传输到本地服务器。
这是我的代码:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Net::SFTP::Foreign;
my $host = "host.ip.address.here";
my $user = "username";
my $pass = "password";
my $path = "/path/to/the/remote/server/directory";
my $local_dir = "/local/dir/path/";
my $sftp = Net::SFTP::Foreign->new(host=>$host , user=>$user , password=>$pass);
$sftp->die_on_error("Unable to establish SFTP connection");
$sftp->setcwd($path) or die "unable to change cwd: " . $sftp->error;
my @file = $sftp->ls($path);
print Dumper(\@file);
$sftp->mget("$path/test*.csv", $local_dir);
我的远程服务器中有文件列表 -
test123.csv
test234.csv
test341.csv
test890.csv
test765.csv
test110.csv
这里的事情是我想从 $path
目录获取最新的文件,该文件基于修改日期。
(我找不到解决方案)
有什么命令可以实现这个功能吗?
我发现的其他方法是循环遍历@files
数组,但如何获取最新文件?此外,我不想循环,因为 $path
有很多文件,需要很长时间才能执行。
所以我想使用单个命令获取远程文件。有什么想法吗?
最佳答案
看看 docs 的 $sftp->stat($path_or_fh) 方法
它返回有关文件的属性列表,包括 mtime。您必须获取提取的每个文件的 mtime,并通过最新文件上的代码做出决定。
关于Perl Net::SFTP::Foreign 从远程服务器获取最新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58612029/