perl - 使用 Net::OpenSSH 将文件从远程计算机传输到本地计算机

标签 perl ssh sftp file-transfer

我已经构建了一个脚本,该脚本应该将文件从远程计算机获取到本地计算机。

#!/usr/bin/perl

use strict;
use warnings;

use Net::OpenSSH;
use Data::Dumper;

my $local_dir = "/LOCAL/DIR/LOCATION/"
print "[LOCAL DIR]-> $local_dir\n";

my $remote_dir = "/REMOTE/DIR/LOCATION/";
print "[REMOTE DIR]-> $remote_dir\n";

my ($host, $user, $password) = ("remote.machine.ip.address", "userid", "password");

my $ssh = Net::OpenSSH->new($host,
                            user => $user,
                            password => $passwd,
                            master_opts => [-o => "StrictHostKeyChecking=no"]
);
$ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;

my @file = $ssh->capture("cd $remote_dir && ls -1tr | grep Report | tail -1");
print "[FILE]:\n".Dumper(\@file);

$ssh->scp_get({glob => 1}, "$remote_dir$file[0]", $local_dir)
        or die "scp failed: " . $ssh->error;

undef $ssh;

在上面的代码中,它能够打印@file的Dumper值,但无法在本地系统中获取该文件。

这是最后抛出的错误:

[FILE]:
$VAR1 = [
          'Report_Managable_20200705.csv
'
        ];
scp: /REMOTE/DIR/LOCATION/Report_Managable_20200705.csv
protocol error: expected control record
scp failed: scp failed: child exited with code 1 at file_get_test.pl line 22.

任何人都可以帮我解决这个问题吗? TIA。

最佳答案

$ssh->capture() 返回的列表在每个项目的末尾都有新行。尝试使用 chomp @file 删除换行符。

关于perl - 使用 Net::OpenSSH 将文件从远程计算机传输到本地计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62762706/

相关文章:

c# - Renci ssh.net 无法连接,因为目标机器主动拒绝

linux - 如何修复:安装 XML::Simple 时未安装 'YAML'?

Perl:意外的 $_ 行为

python - 主管和 perlbrew

perl - 如何说服Devel::Trace打印BEGIN-block语句?

linux - 只允许一台设备通过SSH访问服务器

php - ssh2_auth_password 即使密码错误也总是返回 true

ubuntu - Cygwin SSH 到 Amazon EC2 Ubuntu 挂起

cocoa - 处理非常大的 SFTP 上传 - Cocoa

ubuntu中wordpress自动更新的apache和sftp权限