【问题标题】:Print remote servers FILENAME, MTIME in the screen using Net::SFTP::Foreign Perl Module使用 Net::SFTP::Foreign Perl 模块在屏幕上打印远程服务器 FILENAME、MTIME
【发布时间】:2020-02-25 09:36:59
【问题描述】:

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

这是脚本。

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 作为值,这意味着它的成功。

即使在 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 中获取这些值。

【问题讨论】:

    标签: perl sftp filemtime data-dumper


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢@Miguel 的建议。我通过添加这些行修改了脚本。 my $a1 = Net::SFTP::Foreign::Attributes->new(); foreach my $file( @files ){ my $a2 = $a1->stat($file) or die "remote stat command failed: ".$sftp->status; my $size = $a2->size; my $mtime = $a2->mtime; print "file:$file, size:$size, mtime:$mtime\n"; }。但这给我带来了错误Can't locate object method "stat" via package "Net::SFTP::Foreign::Attributes" at sftp_test.pl line 31.。有什么想法吗?
    • 不要那样称呼stat。您不必创建 Net::SFTP::Foreign::Attributes 实例。从$sftp->stat($path_or_fh) 电话中获取,就像我写的那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    相关资源
    最近更新 更多