【问题标题】:Create .listing file using use Net::SFTP::Foreign使用使用 Net::SFTP::Foreign 创建 .listing 文件
【发布时间】:2013-09-21 12:28:12
【问题描述】:

以下程序正在从 SFTP 服务器下载文件,但我想从 sftpdirectory 的文件列表中的 localdirectory 创建一个 .listing 文件。如何使用 Net::SFTP::Foreign 创建它?

#!/usr/bin/perl

use strict;
use warnings;
use Net::SFTP::Foreign;

my $sftp = Net::SFTP::Foreign->new(
    'username@hostname',
    password => 'password',
    more     => ['-v']
);

$sftp->get('sftpdirectory/data.zip', 'localdirectory')
  or die "unable to retrieve copy: ".$sftp->error;

$sftp->disconnect;

【问题讨论】:

    标签: perl sftp perl-module


    【解决方案1】:

    将所有远程条目检索到数组中并将其转储的简单解决方案:

    # untested!
    my $ls = $sftp->ls('sftpdirectory')
        or die "ls failed: " . $sftp->error;
    open my $fh, '>', 'localdirectory/listing' or die "unable to create file: $!";
    print $fh $_->{longname}, "\n" for @$ls;
    close $fh;
    

    在从远程主机检索远程条目时将远程条目写入.listing 文件的高效内存解决方案:

    open my $fh, '>', 'localdirectory/.listing' or die "unable to create file: $!";
    
    my $wanted = sub {
        print $fh $_[1]{longname}, "\n";
        0;
    }
    
    unless ($sftp->ls('sftpdirectory', wanted => $wanted)) {
        close $fh;
        unlink 'localdirectory/.listing';
        die "ls failed: ". $sftp->error;
    }
    close $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
      • 1970-01-01
      相关资源
      最近更新 更多