Skip to content

Instantly share code, notes, and snippets.

@nathanalderson
Last active June 17, 2021 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanalderson/9711671944cc59ec159a36a53d996821 to your computer and use it in GitHub Desktop.
Save nathanalderson/9711671944cc59ec159a36a53d996821 to your computer and use it in GitHub Desktop.
zsh function for deleting the bad host key from the previous ssh command
# delete the bad host key from the previous ssh command
purgehostkey() {
cmd=$history[$((HISTCMD-1))]
lineno=$(eval $cmd 2>&1 | grep -oP 'known_hosts:\K\d+')
known_hosts=~/.ssh/known_hosts
host=$(sed -n "${lineno}p" $known_hosts | cut --delimiter=' ' --fields=1)
sed -i -e "${lineno}d" $known_hosts
echo "Deleted $host from known_hosts:$lineno 🖥️🔑💥"
eval $cmd -o StrictHostKeyChecking=accept-new
}
╭ 16:32:17 ~
╰ » ssh root@10.27.35.84
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:[redacted].
Please contact your system administrator.
Add correct host key in /home/nalderso/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/nalderso/.ssh/known_hosts:622
ECDSA host key for 10.27.35.84 has changed and you have requested strict checking.
Host key verification failed.
╭ 16:32:38 ~
╰ » purgehostkey
Deleted 10.27.35.84 from known_hosts:622 🖥️🔑💥
Warning: Permanently added '10.27.35.84' (ECDSA) to the list of known hosts.
root@10.27.35.84's password:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment