开发者

Net::SCP / Net::SCP::Expect - how to deal with password vs key authentication

I have a script that works on different clients, and need to SCP files to different hosts. Depending on the combination of client & server, I may need to use password authentication or public key authentication. I cannot really know in advance which one to use.

There are 2 CPAN libraries for SCP that I use:

  • Net::SCP: works with public key authentication only
  • Net::SCP::Expect: works with password authentication only

The problem is that neither library works for both authentications, and I don't know which one to use in advance. Do you know of any way to work with bo开发者_C百科th authentication schemes?


Try one and fail over to the other:

#! /usr/bin/perl

use warnings;
use strict;

use Net::SCP qw/ scp /;
use Net::SCP::Expect;

my @hosts = qw/ host1 host2 host3 /;
my $user  = "YOUR-USERNAME-HERE";
my $pass  = "PASSWORD-GOES-HERE";
my $file  = "file-to-copy";

foreach my $host (@hosts) {
  my $dest = "$host:$file"; 

  my $scp = Net::SCP->new($host, $user);
  unless ($scp->scp($file => $dest)) {
    my $scpe = Net::SCP::Expect->new;
    $scpe->login($user, $pass);

    local $@;
    eval { $scpe->scp($file => $dest) };
    next unless $@;

    warn "$0: scp $file $dest failed:\n" .
         "Public key auth:\n" .
         "    $scp->{errstr}\n" .
         "Password auth:\n" .
         "    $@\n";
  }
}


try Net::OpenSSH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜