使用Net::SFTP::Foreign Perl Module在屏幕中打印远程服务器FILENAME、MTIME



我正在编写一段代码,需要使用Net::SFTP::ForeignPerl模块从远程服务器获取文件。

这是剧本。

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 @files = $sftp->ls($path);
print Dumper(@files);

远程服务器连接工作正常。当我打印$sftp->status时,将0作为值,这意味着它的成功。

即使在转储程序中,我也可以看到来自远程服务器的以下格式的文件。

$VAR1 = [
{
'filename' => 'script.py',
'a' => bless( {
'perm' => 33204,
'size' => 25,
'gid' => 1001,
'flags' => 15,
'mtime' => 1571147796,
'uid' => 1001,
'atime' => 1571655805
}, 'Net::SFTP::Foreign::Attributes' ),
'longname' => '-rw-rw-r--    1 test_vk  test_vk        25 Oct 15 13:56 script.py'
},
{
'a' => bless( {
'flags' => 15,
'mtime' => 1571417934,
'atime' => 1571655769,
'uid' => 1001,
'gid' => 1001,
'size' => 369,
'perm' => 33204
}, 'Net::SFTP::Foreign::Attributes' ),
'longname' => '-rw-rw-r--    1 test_vk  test_vk       369 Oct 18 16:58 script.pl',
'filename' => 'script.pl'
},
{
'longname' => '-rw-r--r--    1 root     root            0 Oct 30 04:32 script123.pl',
'a' => bless( {
'gid' => 0,
'size' => 0,
'perm' => 33188,
'flags' => 15,
'mtime' => 1572409960,
'uid' => 0,
'atime' => 1572409960
}, 'Net::SFTP::Foreign::Attributes' ),
'filename' => 'script123.pl'
},
{
];

我需要的是取出每个文件的修改时间。这需要像"filename, modification_time"一样打印。我怎样才能从Dumper中获取这些值。

根据Net::SFTP::Foreign的文档,有一个stat方法可以调用以获得Net::SFTP::Foreign::Attributes实例:

my $attrs = $sftp->stat($path_or_fh)

然后要mtime,或者你需要的那个。

最新更新