Created
January 11, 2016 22:24
-
-
Save papaben/4c8c4a45d72ff6ac4188 to your computer and use it in GitHub Desktop.
Automatically add several hosts to known_hosts
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
#! /usr/bin/env bash | |
# | |
# In a situation in which you are ssh'ing into a set of servers, and you are | |
# confident about their trustability, but the current server doesn't "know" | |
# them yet. This script will add their signatures into your known_hosts file | |
# if it is not yet there. | |
## | |
declare KNOWN_HOSTS_FILE="$HOME/.ssh/known_hosts" | |
declare server_ip= | |
declare server_ssh_key= | |
for server in $(< ~/Temp/pod4101.hosts); do | |
if ! grep -q "^${server}" "$KNOWN_HOSTS_FILE"; then | |
echo "$server was not found in the user's known_hosts file, automatically adding it..." | |
server_ip=$(dig +short "$server") | |
if [[ -z "$server_ip" ]]; then | |
echo "*** Encountered an unknown error with getting ip address for $server using dig command ***" | |
exit 1 | |
fi | |
server_ssh_key=$(ssh-keyscan -t rsa -H "$server" | awk '{print $2 " " $3}') | |
if [[ -z "$server_ssh_key" ]]; then | |
echo "*** Encountered an unknown error with getting ssh key for $server using ssh-keyscan command ***" | |
exit 1 | |
fi | |
echo "$server,$server_ip $server_ssh_key" >> $KNOWN_HOSTS_FILE | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment