Last active
March 27, 2017 15:11
-
-
Save jceloria/bc88054d01630967b74bcfcfb21c848c to your computer and use it in GitHub Desktop.
ssh wrapper to continuously retry if unavailable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################################################################## | |
# 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