Skip to content

Instantly share code, notes, and snippets.

@nbeernink
Created March 1, 2017 10:23
Show Gist options
  • Select an option

  • Save nbeernink/a4445a2510bc67aec67886ea2ad64c60 to your computer and use it in GitHub Desktop.

Select an option

Save nbeernink/a4445a2510bc67aec67886ea2ad64c60 to your computer and use it in GitHub Desktop.
Make connecting to huge lists of new hosts easier with this simple ssh hostkey importer (using ssh-keyscan)
#!/bin/bash
hostlist=$1
outputfile=$2
if [ -z "${2}" ];then
outputfile=~/.ssh/known_hosts
fi
time while read -r host; do
ip=$(dig +short "$host" 2> /dev/null)
#remove the key from the file
ssh-keygen -R "$host" -f "$outputfile" >/dev/null 2>&1;
ssh-keygen -R "$ip" -f "$outputfile" >/dev/null 2>&1;
#scan for the new key
ssh-keyscan "$host" 2> /dev/null >> "$outputfile" && echo "Added hostkey for $host to $outputfile";
if [ ! -z "${ip}" ]; then
ssh-keyscan "$ip" 2> /dev/null >> "$outputfile" && echo "Added hostkey for $ip to $outputfile";
fi
done < "$hostlist"
sort -u "$hostlist" -o "$hostlist"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment