Skip to content

Instantly share code, notes, and snippets.

@Aeon
Forked from 9point6/ssh-retry.sh
Last active May 4, 2017 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aeon/a983d512f9634e0af130d62afdd28f3a to your computer and use it in GitHub Desktop.
Save Aeon/a983d512f9634e0af130d62afdd28f3a to your computer and use it in GitHub Desktop.
Keep retrying SSH connection until success (Useful for waiting for VMs to boot)
#!/bin/bash
if [ -z "$1" ]; then
echo '' && echo 'Please also provide server name as in config file...' &&
exit 1
fi
retries=0
repeat=true
today=$(date)
while "$repeat"; do
((retries+=1)) &&
echo "Try number $retries..." &&
today=$(date) &&
ssh "$@" &&
repeat=false
if "$repeat"; then
sleep 5
fi
done
echo "Total number of tries: $retries"
echo "Last connection at: $today"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment