I have a file that contains ~1000 hosts, one host per line. For each host, I want to check if an entry for the host already exists in the known_hosts
file. If one does not, I want to add the host to the known_hosts
file. Otherwise, I want to update the existing entry. I would like to use the -H
option to hash all entries in known_hosts
. I have already run the command ssh-keygen -H ~/.ssh/known_hosts
to hash the existing entries.
Here is a simple loop that I put together:
while read LINE; do
# Remove entry if it exists
ssh-keygen -R $LINE
# Append new hashed key to file
ssh-keyscan -H $LINE >> ~/.ssh/known_hosts
done < $HOST_FILE
Is this method secure? If not, what is the recommended way of doing this? The reason I am doing this is to disable typing "yes" 1000 times to allow every new host to be added to known_hosts
for scripting purposes.