【问题标题】:Net::SCP / Net::SCP::Expect - how to deal with password vs key authenticationNet::SCP / Net::SCP::Expect - 如何处理密码与密钥身份验证
【发布时间】:2011-01-03 01:20:32
【问题描述】:

我有一个适用于不同客户端的脚本,需要将文件 SCP 发送到不同的主机。根据客户端和服务器的组合,我可能需要使用密码身份验证或公钥身份验证。我无法真正提前知道该使用哪一个。

我使用了 2 个用于 SCP 的 CPAN 库:

  • Net::SCP:仅适用于公钥身份验证
  • Net::SCP::Expect:仅适用于密码验证

问题是这两个库都不适用于两种身份验证,而且我不知道提前使用哪一个。您知道使用这两种身份验证方案的任何方法吗?

【问题讨论】:

    标签: perl scp


    【解决方案1】:

    尝试一个并故障转移到另一个:

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

    【讨论】:

    • 其实Net::SCP不支持非标准端口:-(
    【解决方案2】:

    试试 Net::OpenSSH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2013-02-04
      • 2018-02-27
      • 1970-01-01
      • 2015-12-08
      • 2017-06-21
      相关资源
      最近更新 更多