Created
May 13, 2017 17:32
-
-
Save vandot/2bf15bfe2194eb5c9f4b0b875c8735e7 to your computer and use it in GitHub Desktop.
You have to many dynamic instances? Resolves all key issues and connects to instance the moment is up.
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
#!/usr/bin/env bash | |
declare -x USER="" | |
declare -x HOST="" | |
declare -x KEY1="" | |
declare -x KEY2="" | |
retry_scan() { | |
while true; do | |
KEY1=$(ssh-keyscan -t ecdsa "${1}") | |
if [[ "${KEY1}" != "" ]]; then | |
break | |
else | |
sleep .$(( RANDOM % 10 ))s | |
fi | |
done | |
} | |
retry_ssh() { | |
while true; do | |
if ssh "${1}"; then | |
break | |
else | |
sleep .$(( RANDOM % 10 ))s | |
fi | |
done | |
} | |
get_host() { | |
if [[ "${1}" == *@* ]]; then | |
USER="$(echo "${1}" | cut -f1 -d'@')@" | |
HOST="$(echo "${1}" | cut -f2 -d'@')" | |
else | |
HOST="${1}" | |
fi | |
} | |
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then | |
get_host "${@}" | |
KEY1=$(ssh-keyscan -t ecdsa "${HOST}") | |
if [[ "${KEY1}" == "" ]]; then | |
retry_scan "${HOST}" | |
fi | |
KEY2=$(ssh-keygen -q -F "${HOST}") | |
if [[ "${KEY2}" == "" ]]; then | |
echo "${KEY1}" >> ~/.ssh/known_hosts | |
elif [[ $(echo "${KEY1}" | cut -d' ' -f3) != $(echo "${KEY2}" | cut -d' ' -f3) ]]; then | |
ssh-keygen -R "${HOST}" | |
echo "${KEY1}" >> ~/.ssh/known_hosts | |
fi | |
retry_ssh "${USER}${HOST}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment