簡體   English   中英

使用 Net::SFTP::Foreign Perl 模塊在屏幕上打印遠程服務器 FILENAME、MTIME

[英]Print remote servers FILENAME, MTIME in the screen using Net::SFTP::Foreign Perl Module

我正在編寫一個需要使用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 中獲取這些值。

根據Net::SFTP::Foreign的文檔,您可以調用一個stat方法來獲取Net::SFTP::Foreign::Attributes實例:

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

然后詢問mtime或您需要的那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM