I have a hostlist and there are several hosts. I want to use ssh to connect to them. I want to see whether the host is available or not by the time length of ssh takes. If it take long than 5s (which means it is not available). Then stop it, and ssh to next host. Once a host is available, then output the name of host. I previously use nmap, but the IT security told it is not allowed to install it on the desktop on campus.
HOSTS=`cat $host_list`
for line in $HOSTS
do
ssh -o ConnectTimeout=5s $line true >> /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]
then
echo $line > succeed.txt
else
echo $line > fail.txt
fi
done
The above script is all I can do.
The problem is when it is failed, I want to redirect the error message
ssh: connect to host c28-0112-05.ad.mtu.edu port 22: No route to host
. But I still get the error message.