1

我正在尝试编写一个使用 ssh 的简单脚本,但我想先测试 ssh 是否正常工作 - 如果它不起作用,我想知道它为什么不起作用。

这是我正在尝试的:

my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo $host\"");
my $real_output = $output/256;
if($real_output == 2){print "RESPONSE: $host is not working, No address associated to the name \n";}
elsif($real_output == 255){print "RESPONSE: $host is not working (connection timed out after 3 secs, it exists but does not respond) \n";}

这有效:它收集错误并测试连接,但是当我运行它时;当主机名不存在时显示如下:

./testMachines --n invalid_host
warning: Connecting to invalid_host failed: No address associated to the name
RESPONSE: invalid_host is not working, No address associated to the name

或者当主机名确实存在但超时时:

./testMachines --n timeout_host
ssh: FATAL: Connection timed out.  No protocol greeting received in 10 seconds.
RESPONSE: timeout_host is not working (connection timed out after 3 secs, it exists but does not respond)

如何抑制“ssh:FATAL”和“警告”消息?我的印象是 ssh 的“-q”选项可以解决问题,但它没有。

我还尝试了 ssh 上的“-q -q”选项(如手册页上所建议的那样),但没有运气。

我也试过这个:

my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo 2>&1\"");

没有任何运气......有什么想法吗?

基本上我想要的是这样的输出(没有 FATAL 和警告 ssh 消息):

# ./testMachines -n host
RESPONSE: host is not working (connection timed out after 3 secs, it exists but does not respond)

非常感谢!!!

4

1 回答 1

1

不确定您是如何想到添加所有转义的,但这应该可行:

my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo $host\" 2>/dev/null");
于 2012-05-07T18:41:09.993 回答