Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 使用Net::OpenSSH将文件从远程计算机传输到本地计算机_Perl_Ssh_Sftp_File Transfer - Fatal编程技术网

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

Perl 使用Net::OpenSSH将文件从远程计算机传输到本地计算机,perl,ssh,sftp,file-transfer,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/LOCAT

我已经建立了一个脚本,应该从远程机器到本地机器获取一个文件

#!/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
的转储程序值,但无法在本地系统中获取该文件

下面是它在最后抛出的错误:

[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
删除换行符。

谢谢@Hakon。我是如何忘记使用
chomp
:)