Skip to content

Instantly share code, notes, and snippets.

@lvnilesh
Forked from nathanalderson/purgehostkey.zsh
Created June 17, 2021 22:14
Show Gist options
  • Save lvnilesh/f15fc7da0515a6c09c934b0046882618 to your computer and use it in GitHub Desktop.
Save lvnilesh/f15fc7da0515a6c09c934b0046882618 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 [email protected]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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.
[email protected]'s password:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment