Skip to content

Instantly share code, notes, and snippets.

@shreddd
Created May 10, 2017 17:34
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 shreddd/fee9ee85db746570c467cc3fadc67fe4 to your computer and use it in GitHub Desktop.
Save shreddd/fee9ee85db746570c467cc3fadc67fe4 to your computer and use it in GitHub Desktop.
Remove entries from known_hosts files (including variations on the hostname)
#!/bin/bash
# sshrmkey.sh
#
# Little BASH tool to clean up a hostname from ssh known_hosts
#
# Shreyas Cholia - 2017-05-10 shreyas@gmail.com
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
shortname=false
ip=false
show_help() {
echo >&2
echo "Usage: $0 [options...] {hostname}" >&2
echo " -s Clean up shortname" >&2
echo " -i Clean up IP" >&2
echo >&2
echo "sshrmkey.sh uses 'ssh-keygen -R' to clean up hostname in .ssh/known_hosts file" >&2
exit 1
}
while getopts "h?si" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
s) shortname=true
;;
i) ip=true
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
host=$@
echo "ssh-keygen -R $host"
ssh-keygen -R $host
if $shortname; then
host_shortname=$(echo $host | cut -d. -f1)
echo "ssh-keygen -R $host_shortname"
ssh-keygen -R $host_shortname
fi
if $ip; then
host_ip=$(dig +short $host)
echo "ssh-keygen -R $host_ip"
ssh-keygen -R $host_ip
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment