Skip to content

Instantly share code, notes, and snippets.

@xxorax
Last active July 15, 2024 07:37
Show Gist options
  • Save xxorax/e46341cf57fad0bdd80a to your computer and use it in GitHub Desktop.
Save xxorax/e46341cf57fad0bdd80a to your computer and use it in GitHub Desktop.
Unhash the hostnames hashed in your known_hosts file by passing their names.
#!/bin/bash
# based on http://unix.stackexchange.com/questions/175071/how-to-decrypt-hostnames-of-a-crypted-ssh-known-hosts-with-a-list-of-the-hostna/175199#175199
function replace {
host="$1"
found=$(ssh-keygen -F "$host" 2>/dev/null | grep -v '^#' | sed "s/^[^ ]*/$host/")
if [ -n "$found" ]; then
ssh-keygen -R "$host" &>/dev/null
echo "$found" >>~/.ssh/known_hosts
echo "Found and replaced: $host"
else
echo "Not found: $host"
fi
}
if [ "$#" -eq 0 ]; then
echo "Enter one hostname per line, or input a filename ( < file )."
while read host comment; do
replace "$host"
done
exit
fi
while (( "$#" )); do
replace "$1"
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment