Skip to content

Instantly share code, notes, and snippets.

@Ran-Xing
Created May 3, 2022 03:49
Show Gist options
  • Save Ran-Xing/44160469ba2b38ac9a2d19e64e100e63 to your computer and use it in GitHub Desktop.
Save Ran-Xing/44160469ba2b38ac9a2d19e64e100e63 to your computer and use it in GitHub Desktop.
ssh del known hosts
#!/usr/bin/env bash
if echo "$1" | tr -d " " | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" > /dev/null; then
IP="$(echo "$1" | tr -d " " | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$")"
VALID_CHECK=$(echo "$IP"|awk -F. '$1<=255&&$2<=255&&$3<=255&&$4<=255{print "yes"}')
if [ "${VALID_CHECK:-no}" == "yes" ]; then
echo "IP [$IP] available."
else
echo "IP [$IP] not available!"
exit 1
fi
else
if grep "$1" ~/.ssh/config > /dev/null; then
IP="$(grep -A 3 "$1" ~/.ssh/config | grep -m 1 HostName | grep -Eo '([^0-9]|\b)((1[0-9]{2}|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])\.){3}(1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])([^0-9]|\b)' | tr -d " ")"
echo "IP [$IP] available."
else
echo "IP [$IP] not available!"
exit 1
fi
fi
sed -i "" "/$IP/d" ~/.ssh/known_hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment