首页 > 解决方案 > 使用 Net::SFTP::Foreign Perl 模块在屏幕上打印远程服务器 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->status0价值时,这意味着它的成功。

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

$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 中获取这些值。

标签: perlsftpfilemtimedata-dumper

解决方案


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

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

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


推荐阅读