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
$hostname variables with the appropriate values.
#!/bin/bash user=root hostname=10.0.0.1 i="0" while [ $i -lt 100 ] do ssh -o ConnectTimeout=10 $user@$hostname sleep 12s i=$[$i+1] done
If you need to end the script, you can use Ctrl+C. But the loop will end automatically if an SSH attempt is successful.