1

I wrote the following script in Perl (I'm new to Perl) which attempts to send files via SCP to a remote machine. I'm trying to use Expect to pass the password along. The script so far, looks like this:

#!/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);

However, the transfer doesn't seem to happen. Moreover, no errors are thrown. Where have I gone wrong?

rahuL
  • 3,330
  • 11
  • 54
  • 79
  • is there any good reason you are authenticating by password instead of private key? – michas Oct 05 '13 at 09:37
  • @michas - This is due to the fact that the users are to be given a choice to opt for either option - private key or password. – rahuL Oct 05 '13 at 09:41

2 Answers2

4

use Net::OpenSSH or Net::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;
salva
  • 9,943
  • 4
  • 29
  • 57
3

Try this:

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

Lemme know if that works for you..

rahuL
  • 3,330
  • 11
  • 54
  • 79
  • Still no reason to call your scp_backup subroutine with `&` -- cf.http://stackoverflow.com/questions/1347396/when-should-i-use-the-to-call-a-perl-subroutine. – fenway Oct 05 '13 at 17:43
  • Thanks, but Net::SCP::Expect is working only for remote to local and local to remote. I want something which should work for remote to remote also. – Yash Aug 25 '18 at 16:15