Skip to content

Instantly share code, notes, and snippets.

@slumos
Created June 22, 2018 22:00
Show Gist options
  • Save slumos/a5fca4bafa7628601eb79b0359e3ae19 to your computer and use it in GitHub Desktop.
Save slumos/a5fca4bafa7628601eb79b0359e3ae19 to your computer and use it in GitHub Desktop.
reset known_hosts entry for a hostname
#! /usr/bin/env zsh
if [[ $# != 1 ]]; then
print Usage: kh-reset hostname
exit 1
fi
# Expand the different hostnames and IP
if [ -x /usr/bin/dscl ]; then
# macOS
hosts="$hosts $(dscl . read /Hosts/$1 | awk '/^IPAddress/ { print $2 }')"
else
# sanity
hosts="$1 $(getent hosts $1)"
fi
# Remove all matching entries from known_hosts
for h in ${=hosts}; do ssh-keygen -R $h >& /dev/null; done
# Grab the new key into known_hosts
ssh-keyscan -tdsa,rsa ${=hosts} >> $HOME/.ssh/known_hosts 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment