I use SSH to run some commands on multiple remote machines in a for loop. It executes the same command(s) for a list of IP addresses. Some of the IP addresses might be unreachable, so I used the ConnectTimeout option.
However, my script didn't work the way I wanted. Actually it got stuck at the first unreachable IP instead of giving up and trying the next IP address on my list.
Here is the relevant part of my script:
for ip in ${IP} ; do
ssh -o BatchMode=yes \
-o StrictHostKeyChecking=no \
-o ConnectTimeout=10 \
-l ${USERNAME} \
${SCRIPT_HOST} \
"${COMMAND} -i $ip || echo timeout" \
>> ./myscript.out
done
It is working fine for reachable IPs, but if a specific IP is down, it waits for a while (much more than 10s, maybe 35-40 seconds) and displays an error message to my terminal:
ERROR connecting : Connection timed out
So I'm wondering which option I didn't use correctly.
<your command> 2>/dev/null