Created
March 1, 2017 10:23
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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