Files
q-tools/web/ssh-remove-known-host
2019-05-07 07:31:32 +03:00

38 lines
992 B
Bash
Executable File

#!/bin/bash
function helpexit() {
echo "Remove hosts from ~/.ssh/known_hosts"
echo "-f no questions."
exit
}
for (( i=1; i<=$#; i++ )); do
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-h" ]] && helpexit
[[ "${!i}" = "-f" ]] && { FORCE=1; continue; }
HOST="${!i}"
done
[[ -z "$HOST" ]] && helpexit
REALHOST=$( ssh -G "$HOST" 2> /dev/null | awk '/^hostname / { print $2 }' )
PORT=$( ssh -G "$HOST" 2> /dev/null | awk '/^port / { print $2 }' )
[[ -z "$REALHOST" ]] && REALHOST="$HOST"
IP=$( getent hosts "$REALHOST" | awk '{ print $1 }' )
[[ "$FORCE" -ne 1 ]] && {
echo "Sure to remove ${REALHOST}:${PORT} (IP: ${IP}) from known_hosts? [y/N]"
read resp
} || {
resp=y
}
[[ "$resp" = "y" ]] && {
echo ssh-keygen -R "[$REALHOST]:$PORT"
ssh-keygen -R "[$REALHOST]:$PORT"
[[ -n "$IP" ]] && {
echo ssh-keygen -R "[$IP]:$PORT"
ssh-keygen -R "[$IP]:$PORT"
}
}