Created
February 8, 2013 15:45
-
-
Save kfox/4739828 to your computer and use it in GitHub Desktop.
An easy way to remove old SSH host keys from your ~/.ssh/known_hosts file. Useful if you often re-image systems.
This file contains hidden or 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
# forget.sh: remove matching lines from ~/.ssh/known_hosts | |
# usage: forget <hostname|IP|substring> [...] | |
function forget { | |
known_hosts=~/.ssh/known_hosts | |
for host in $* | |
do | |
host=$(echo "${host}" | sed -e 's/^ssh:\/\///' -e 's/^.*@//') | |
line=$(awk '{ print $1 }' ${known_hosts} | grep -n "${host}" | sed 's/:.*$//g' | xargs) | |
while [ -n "${line}" ] | |
do | |
l=$(echo $line | awk '{ print $1 }') | |
echo -n "==> removing from ${known_hosts}: " | |
awk '{ if (NR==n) print $1 }' n=$l ${known_hosts} | |
sed -i '.old' "${l}d" ${known_hosts} | |
line=$(awk '{ print $1 }' ${known_hosts} | grep -n "${host}" | sed 's/:.*$//g' | xargs) | |
done | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just include this in my ~/.bashrc.