SCP使用perl脚本传输文件

rah*_*huL 1 perl scp perl-module

我用 Perl 编写了以下脚本(我是 Perl 新手),它尝试通过 SCP 将文件发送到远程计算机。我正在尝试使用 Expect 来传递密码。到目前为止,脚本看起来像这样:

#!/usr/bin/perl

use Net::FTP;
use Net::SCP;
use Net::SCP::Expect;
sub scp_backup{
        $n = scalar(@_);
        if ($n == 7)
        {        my $scp_conn = Net::SCP->new($ip, $username) or die "\nCan't open $ip";
                 #die
                 my $dest="/my/remote/location/";
                 my $testfile = "/pah/to/my/file/testing.txt";
                 unless ($scp_conn->scp($testfile => $dest))
                 {       my $scp_conn_ex = Net::SCP::Expect->new;
                         $scp_conn_ex->login($username, $password);
                 }

        }
        else
        {
                print "\nNot enough args\n\n";
        }
        print "\nTotal items passed:$n\n";
}


$name = "testuser";
$tfr_type = "scp";
$ip = "XX.XX.XX.XX";
$username = 'testuser_name';
$password = 'testpass';
&scp_backup($name, $tfr_type, $ip, $username, $password);
Run Code Online (Sandbox Code Playgroud)

然而,转移似乎并没有发生。此外,不会引发任何错误。我哪里出错了?

sal*_*lva 5

使用Net::OpenSSHNet::SSH::Any

use Net::OpenSSH;
my $ssh = Net::OpenSSH->new($host, user => $user, password => $password);
$ssh->scp_put($local_path, $remote_path)
    or die "scp failed: " . $ssh->error;
Run Code Online (Sandbox Code Playgroud)