Skip to content

Instantly share code, notes, and snippets.

@LarsKumbier
Created February 15, 2019 10:06
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 LarsKumbier/cb999caf2743a2e81061ad05887a1fb0 to your computer and use it in GitHub Desktop.
Save LarsKumbier/cb999caf2743a2e81061ad05887a1fb0 to your computer and use it in GitHub Desktop.
This will update the remote ssh keys and reset the known_hosts file after a redeploy of new machines
#!/usr/bin/env bash
##
## This will update the remote ssh keys and reset the known_hosts file after a redeploy of a new VM
##
## will return the found *.example.com host names from an ansible file
## - alternatively, put your own logic here or give a fixed list:
## HOSTS=( host1.example.com host2.example.de )
HOSTS=( $(grep -o -e "[a-zA-Z0-9\-]*\.example\.com" inventory/heidelberg/inventory) )
if [[ -z ${SSHUSER} ]]; then
echo -n ssh user:
read SSHUSER
else
echo "SSH User: ${SSHUSER}"
fi
if [[ -z ${SSHPASS} ]]; then
echo -n ssh password:
read -s SSHPASS
echo
fi
export SSHPASS="${SSHPASS}"
if [[ -z ${SSHPASS} || -z ${SSHUSER} ]]; then
echo "I require an ssh user (or \$SSHUSER) and ssh password (or \$SSHPASS)" >&2
exit 1
fi
for host in "${HOSTS[@]}"; do
echo "Updating ${host}"
ssh-keygen -R "${host}" &> /dev/null || true
ssh-keyscan "${host}" >> "${HOME}/.ssh/known_hosts" 2> /dev/null
sshpass -e ssh-copy-id ${SSHUSER}@${host} > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment