Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AdrianAcala/2962c555ab3ff0066f9a095673c88a7b to your computer and use it in GitHub Desktop.
Save AdrianAcala/2962c555ab3ff0066f9a095673c88a7b to your computer and use it in GitHub Desktop.
Removes all IPs from known_hosts except IPs that start with 192 or have an FQDN
while IFS= read -r line; do
if [[ $line =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[[:space:]] ]]; then
ip=$(echo "$line" | awk '{print $1}')
if [[ $ip =~ ^192\. ]]; then
echo "$line"
fi
else
echo "$line"
fi
done < ~/.ssh/known_hosts > ~/.ssh/known_hosts.new && mv ~/.ssh/known_hosts.new ~/.ssh/known_hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment