2013-10-05 3 views
1

필자는 SCP를 통해 원격 컴퓨터로 파일을 보내려고하는 Perl에 다음 스크립트를 작성했습니다. Expect를 사용하여 암호를 전달하려고합니다. 지금까지 스크립트는 다음과 같습니다.SCP는 펄 스크립트를 사용하여 파일을 전송합니다.

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

그러나 전송이 발생하지 않는 것 같습니다. 또한 오류가 발생하지 않습니다. 내가 어디로 잘못 갔니?

+0

을 당신이 대신 암호로 인증 된 모든 좋은 이유가 개인 키? – michas

+0

@michas - 이는 사용자가 개인 키 또는 비밀 번호 중 하나를 선택할 수있는 선택권이 주어 졌기 때문입니다. – rahuL

답변

1

이 시도 : 그 당신을 위해 작동하는 경우

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

맞쳐 알고 ..

+0

'&'- scsw_backup 서브 루틴을 호출 할 이유가 없어도 - cf.http : //stackoverflow.com/questions/1347396/when-should-i-use-the-to-call-a-perl-subroutine. – fenway

3

사용 Net::OpenSSH 또는 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; 
관련 문제