Last active
June 17, 2021 22:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
╭ 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