Net :: SCP / Net :: SCP :: Expect - how to deal with password and key verification

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

There are 2 CPAN libraries for SCP that I use:

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

The problem is that no library works for both authentications, and I don't know which one should be used in advance. Do you know any way to work with both authentication schemes?

+3
source share
2 answers

Try one and go to another:

#! /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";
  }
}
+5
source

try Net :: OpenSSH

0
source

Source: https://habr.com/ru/post/1727803/


All Articles