1

I am writing a code which need to get the file from remote server using Net::SFTP::Foreign Perl module.

Here is the script.

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);

Remote server connection works fine. When I print $sftp->status gives 0 as value which means its success.

Even in the Dumper I could see the files from the remote server in below format.

$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'
          },
          {
        ];

What I need is take out the modification time of each of the file. This needs to be printed like "filename, modification_time". How can I take these values from the Dumper.

vkk05
  • 3,137
  • 11
  • 25

1 Answers1

3

According to the documentation of Net::SFTP::Foreign, there is a stat method you can call to get a Net::SFTP::Foreign::Attributes instance:

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

And then ask for the mtime, or the one you need.

Miguel Prz
  • 13,718
  • 29
  • 42
  • 1
    Thanks for suggestion @Miguel. I have modified the script by adding these lines. ```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"; }```. But this throws me the error ```Can't locate object method "stat" via package "Net::SFTP::Foreign::Attributes" at sftp_test.pl line 31.```. Any idea? – vkk05 Oct 30 '19 at 06:46
  • 1
    Don't call `stat` that way. You don't have to create a `Net::SFTP::Foreign::Attributes` instance. Get it from `$sftp->stat($path_or_fh)` call, like I wrote. – Miguel Prz Oct 30 '19 at 07:16