Skip to content

Resetting SSH keys script

I have for some time had the need for SSH’ing to a lot of customer machines, lots of those have the same internal IP address ex. 192.168.1.100, so everytime I initialized a SSH connection my terminal comes up with the following message:

➜ ~ ssh root@192.168.1.2
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
02:1b:b7:c8:90:7d:08:51:f2:27:dc:7b:7e:d6:35:c4.
Please contact your system administrator.
Add correct host key in /Users/USER/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/USER/.ssh/known_hosts:16
RSA host key for 192.168.1.2 has changed and you have requested strict checking.
Host key verification failed.

So now you have to do the tedious sudo nano ~/.ssh/known_hosts locate line 16 and delete that. Its very time consuming. So i made a script for this making an everyday admin life easier

Put the following code a file using nano call it ex. SSH-key-remover.sh and chmod +x SSH-key-remover.sh

#! /bin/bash

while
[[ -z “$IP” ]]
do
clear
echo ” ”
echo “=====================================================================”
echo ” Delete SSH Key”
echo “=====================================================================”
echo ” ”
echo “Type in the IP address of the key that needs to be removed:”
read IP
done

X=0
until

grep -q “$IP” ~/.ssh/known_hosts
do

clear
echo ” ”
echo “=====================================================================”
echo ” Delete SSH Key”
echo “=====================================================================”
echo ” ”

echo “IP was not found in SSH known_hosts file”
echo ” ”
echo “Please type the correct IP address”
echo “or exit with ctrl-c;”
read IP
if [[ -z “$IP” ]]
then
echo “Exiting due to nothing has been typed. Try Again.”
exit 1
fi
let X=$X+1

if
[ $X == 4 ]
then
echo “The IP address is not there, even after 3 attempts.”
echo “Exiting script…!!”
exit 0
fi
done

clear
echo ” ”
echo “=====================================================================”
echo ” Delete SSH Key”
echo “=====================================================================”
echo ” ”
echo “The key for “$IP” has been found!!!”
echo ” ”
echo “Are you sure you want to delete the key?”
echo “y / n”
read delete

if [ $delete = y ]
then

echo “Removing “$IP”’s SSH key”
sleep 5
grep -v “$IP” ~/.ssh/known_hosts > ~/.ssh/known_hostsnew
mv ~/.ssh/known_hostsnew ~/.ssh/known_hosts
chown $USER ~/.ssh/known_hosts

if
[ $? != 0 ]
then
clear
echo ” ”
echo “=====================================================================”
echo ” Delete SSH Key”
echo “=====================================================================”
echo ” ”
echo “The file could not be copied, check the permissions to the files ~/.ssh/known_hosts and ~/.ssh/known_hostsnew”
exit 1
fi

clear
echo ” ”
echo “=====================================================================”
echo ” Delete SSH Key”
echo “=====================================================================”
echo ” ”
echo “Key successfully removed.”
echo “Exiting!!!”
exit 0

else
clear
echo ” ”
echo “=====================================================================”
echo ” Delete SSH Key”
echo “=====================================================================”
echo ” ”
echo “Exiting script. Run this script again to remove other keys.”
exit 0
fi

Published inAppleScripting

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *