Skip to content

Instantly share code, notes, and snippets.

@jceloria
Last active March 27, 2017 15:11
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 jceloria/bc88054d01630967b74bcfcfb21c848c to your computer and use it in GitHub Desktop.
Save jceloria/bc88054d01630967b74bcfcfb21c848c to your computer and use it in GitHub Desktop.
ssh wrapper to continuously retry if unavailable
########################################################################################################################
# ssh wrapper to continuously retry if unavailable
function ssh() {
local next prev host port
for ((i=1;i<=$#;++i)); do
next=$((i+1)); [[ $((i-1)) -eq 0 ]] || prev=$((i-1))
case "${!i}" in
-p*) [[ "${!i}" =~ -p$ ]] && port=${!next} || port=${!i//-p} ;;
*@*) host=${!i##*@} ;;
-*) true ;;
*) [[ ! "${host}" ]] && [[ ! "${!prev}" =~ ^- ]] && host=${!i} ;;
esac
done
echo "Waiting for ssh port '${port:=22}' on '${host}' to become available:"
( bash -c "exec {FD}<>/dev/tcp/${host}/${port}" ) &> /dev/null
while [[ $? -ne 0 ]]; do
sleep 2; ( bash -c "exec {FD}<>/dev/tcp/${host}/${port}" ) &> /dev/null
done
$(type -P ssh) "$@"
}
########################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment