SSH Loop Script for Linux

This script is ideal for when you are expecting a server or other device to come online and you don’t want to keep attempting to SSH into it.

The script works by trying to SSH for 10 seconds, before aborting and then trying again after a 2 second break. It will try 100 different times and should last a total of 20 minutes, unless the device comes online and the SSH is successful.

Just replace the $user and $hostname variables with the appropriate values.



while [ $i -lt 100 ]
ssh -o ConnectTimeout=10 $user@$hostname
sleep 12s

If you need to end the script, you can use Ctrl+C. But the loop will end automatically if an SSH attempt is successful.

Leave a Comment

Your email address will not be published. Required fields are marked *