终止密码提示在Perl中在一定的时间后在Perl中
问题描述:
我试图在没有SSH公钥的服务器的一定时间后,在Perl脚本中的scp命令期间退出密码输入提示。在此脚本中,我正在使用back-tick将文件从一台服务器复制到另一台服务器,但脚本停留在密码提示处,即使在分配的超时后也不会退出。终止密码提示在Perl中在一定的时间后在Perl中
my $test = '';
my $exit_value = '';
eval {
my $timeout = 2;
local $SIG{ALRM} = sub { die "timeout\n" };
alarm($timeout);
$test = `scp foo.txt [email protected]:/`;
$exit_value = $? >> 8;
alarm(0);
}
if ([email protected]) {
print "Time out";
}
有没有办法处理上述情况?
答
我使用IPC::Run
module来达到上述目的,在一定时间后终止密码提示。
所以我当前的代码看起来像这样
my $in = '';
my $out = '';
my $err = '';
my @cmd = ('scp' , 'foo.txt' , '[email protected]:/');
eval {
run \@cmd, \$in, \$out, \$err,IPC::Run::timeout(5)
or die "SSH Error: $? $err\n";
};
if ([email protected]) {
print "TimeOut";
}
答
从非交互式会话调用时,应该设置BatchMode以完全禁用密码提示。
$test = `scp -o BatchMode=yes foo.txt [email protected]:/`;
见下
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''.
答
你也可以使用下面的参数来禁用使用host_key检查: -
$测试= SCP -o批处理模式= YES -o StrictHostKeyChecking = no foo.txt bar @ baz:/;