Skip to content

Instantly share code, notes, and snippets.

@dclausen
Forked from 9point6/ssh-retry.sh
Created October 18, 2016 15:21
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 dclausen/52ce71ff3f1f0501ba411e8b53ab5a2d to your computer and use it in GitHub Desktop.
Save dclausen/52ce71ff3f1f0501ba411e8b53ab5a2d to your computer and use it in GitHub Desktop.
Keep retrying SSH connection until success (Useful for waiting for VMs to boot)
#!/usr/bin/env bash
# Check we've got command line arguments
if [ -z "$*" ] ; then
echo "Need to specify ssh options"
exit 1
fi
# Start trying and retrying
((count = 100))
while [[ $count -ne 0 ]] ; do
ssh $*
rc=$?
if [[ $rc -eq 0 ]] ; then
((count = 1))
fi
((count = count - 1))
done
# Print a message if we failed
if [[ $rc -ne 0 ]] ; then
echo "Could not connect to $* after 100 attempts - stopping."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment