I have a bash script that downloads information from a sever using rsync. This script runs on remote client machines. Occasionally, I will get error messages because the server IP may have changed or we have a new server. So for example, I will get a message like this:
Warning: the ECDSA host key for 'example.com' differs from the key for the IP address 'x.x.x.x.'
Offending key for IP in /home/<user>/.ssh/known_hosts:5
Matching host key in /home/<user>/.ssh/known_hosts:6
And also messages like:
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
with of course a whole bunch of messages. I want to delete the key using ssh-keygen etc. This happened recently because we moved our server. I don’t want to delete the key if it has happened already and I don’t want to do all this manually to 30 machines. So how do I catch those strings shown above and if they exist, run the ssh-keygen command. Something like this for the first case (to remove the cached key):
if grep -<options> "Warning: the ECDSA host key for" <output>
then
ssh-keygen -R x.x.x.x
else
echo "You are all set!"
fi
Note: This is fairly secure because the script is downloaded dynamically and deletes itself once it runs.
2