Question

I'm trying to write a simple script that uses ssh, but I want to test that ssh is working first -if it doesnt work I want to know why it's not working.

Here is what I'm trying:

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";}

This works: it collects the error and tests the connection, but when I run it; it shows the following when the name of the host does not exist:

./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

or when the host name does exist but it's timing out:

./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)

How do I suppress the "ssh: FATAL" and "warning" messages? I was under the impression that the '-q' option for ssh will do the trick, but it didnt.

I've also tried the '-q -q' option on ssh (as suggested on the man pages) but no luck.

I've also tried this:

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

without any luck... any ideas?

Basically what I want is the output to be like this (without the FATAL and warning ssh messages):

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

Thanks a lot!!!

Dan

Was it helpful?

Solution

Not sure how you got the idea to add all that escaping, but this should work:

my $output = system("ssh -q -o ConnectionTimeout=3 -o BatchMode=yes $host \"echo $host\" 2>/dev/null");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top