#!/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 HOST=$( ssh -G "$HOST" | awk '/^hostname / { print $2 }' ) IP=$( getent hosts "$HOST" | awk '{ print $1 }' ) [[ "$FORCE" -ne 1 ]] && { echo "Sure to remove $HOST (IP: $IP) from known_hosts? [y/N]" read resp } || { resp=y } [[ "$resp" = "y" ]] && { set -x ssh-keygen -R "$HOST" ssh-keygen -R "$IP" }