在一定时间后在 perl 中的 scp 期间终止密码提示
terminating the password prompt during scp in perl after certain amount of time
我正在尝试在服务器没有 ssh public 密钥的特定时间后,在 perl 脚本中的 scp 命令期间退出密码输入提示。在此脚本中,我使用反引号将文件从一台服务器复制到另一台服务器,但脚本卡在密码提示上,即使在指定的超时后也不会退出。
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";
}
有什么办法可以处理上述情况吗?
从非交互式会话调用时,您应该设置 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''.
您还可以使用以下参数禁用 host_key 检查:-
$test = scp -o BatchMode=yes -o StrictHostKeyChecking=no foo.txt bar@baz:/;
我利用IPC::Run
module实现了上述目的,在一定时间后终止密码提示。
所以我当前的代码是这样的
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";
}
我正在尝试在服务器没有 ssh public 密钥的特定时间后,在 perl 脚本中的 scp 命令期间退出密码输入提示。在此脚本中,我使用反引号将文件从一台服务器复制到另一台服务器,但脚本卡在密码提示上,即使在指定的超时后也不会退出。
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";
}
有什么办法可以处理上述情况吗?
从非交互式会话调用时,您应该设置 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''.
您还可以使用以下参数禁用 host_key 检查:-
$test = scp -o BatchMode=yes -o StrictHostKeyChecking=no foo.txt bar@baz:/;
我利用IPC::Run
module实现了上述目的,在一定时间后终止密码提示。
所以我当前的代码是这样的
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";
}