1

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

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

4

2 回答 2

4

使用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;
于 2013-10-05T10:36:25.820 回答
3

尝试这个:

use Net::FTP;
use Net::SCP;
use Net::SCP::Expect;
sub scp_backup{
        $n = scalar(@_);
        if ($n == 7)
        {           my $dest="/my/remote/location/";
                    my $testfile = "/pah/to/my/file/testing.txt";

                    my $scpe = Net::SCP::Expect->new(
                           host => $ip,
                           user => $username,
                           password => $password,
                           auto_yes => 1,
                           verbose  => 1,
                           debug    => 1,
                           timeout_auto => 2,
                        );
                        die "can't scp: $!\n" unless $scpe->scp($testfile, $dest);
        }
        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);

让我知道这是否适合你..

于 2013-10-05T10:18:45.933 回答