Skip to content

Instantly share code, notes, and snippets.

@tcotav
Last active September 24, 2015 01:54
Show Gist options
  • Save tcotav/6914190dba79fad72bd6 to your computer and use it in GitHub Desktop.
Save tcotav/6914190dba79fad72bd6 to your computer and use it in GitHub Desktop.
Clean ssh known_hosts files for a specified user
#!/bin/bash
# ssh_key_clean.sh user site1 site2 site3 site4
user=$1
hostlist=`echo "${@:2}"`
for hhost in $hostlist; do
ip=$(dig +short $hhost)
if [ -z "$ip" ];then
echo "ERROR: couldn't get ip for host $hhost"
exit 2
fi
sudo -H -u $user bash -c "ssh-keygen -R $hhost"
sudo -H -u $user bash -c " ssh-keygen -R $ip"
sudo -H -u $user bash -c "ssh-keyscan -H $ip >> ~/.ssh/known_hosts"
sudo -H -u $user bash -c "ssh-keyscan -H $hhost >> ~/.ssh/known_hosts"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment