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」および「warning」メッセージを抑制するにはどうすればよいですか?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 に答える