【问题标题】:terminating the password prompt during scp in perl after certain amount of time一定时间后,在 perl 中的 scp 期间终止密码提示
【发布时间】:2015-10-11 17:46:15
【问题描述】:

我正在尝试在 perl 脚本中的 scp 命令期间在没有 ssh 公钥的服务器经过一定时间后退出密码输入提示。在此脚本中,我使用反引号将文件从一台服务器复制到另一台服务器,但脚本卡在密码提示符上,即使在指定的超时后也不会退出。

my $test       = '';
my $exit_value = '';
eval {
          my $timeout = 2;
          local $SIG{ALRM} = sub { die "timeout\n" };
          alarm($timeout);
          $test       = `scp foo.txt bar@baz:/`;
          $exit_value = $? >> 8;
          alarm(0);
     }

if ($@) {
    print "Time out";
}

有没有办法处理上述情况?

【问题讨论】:

    标签: perl scp


    【解决方案1】:

    您应该在从非交互式会话中调用时设置 BatchMode 以完全禁用密码提示。

    $test = `scp -o BatchMode=yes foo.txt bar@baz:/`;
    

    见下文

     BatchMode
             If set to ``yes'', passphrase/password querying will be disabled.
             This option is useful in scripts and other batch jobs where no
             user is present to supply the password.  The argument must be
             ``yes'' or ``no''.  The default is ``no''.
    

    【讨论】:

      【解决方案2】:

      您还可以使用以下参数来禁用 host_key 检查:-

      $test = scp -o BatchMode=yes -o StrictHostKeyChecking=no foo.txt bar@baz:/;

      【讨论】:

        【解决方案3】:

        我利用IPC::Runmodule实现了上述目的,在一定时间后终止密码提示。

        所以我现在的代码是这样的

        my $in  = '';
        my $out = '';
        my $err = '';
        my @cmd = ('scp' , 'foo.txt' , 'bar@baz:/');
        
        eval {
                run \@cmd, \$in, \$out, \$err,IPC::Run::timeout(5)
                    or die "SSH Error: $? $err\n";
            };
        if ($@) {
           print "TimeOut";
        }
        

        【讨论】:

          猜你喜欢
          • 2018-05-25
          • 1970-01-01
          • 1970-01-01
          • 2013-08-21
          • 2012-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-03
          相关资源
          最近更新 更多