Script to copy RSA key multiple servers

I recently needed to add my rsa public key to multiple servers where the .ssh directory didn’t already exist nor the .ssh/authorized_keys in addition to the correct permissions for the .ssh directory and authorized_keys file

I found this taken from the below post on how to install it on OS X: http://thesystemisntdown.blogspot.com/2010/03/ssh-copy-id-on-os-x.html

I can’t find an official source for the ssh-copy-id. If you can, please comment a link for me.

Nevertheless, this should get you what you’re looking for:
wget http://blog.christopherpitzer.com/wp-content/uploads/ssh-copy-id -O /usr/bin/ssh-copy-id
sudo chmod +x /usr/bin/ssh-copy-id

Note: ssh-copy-id appends to authorized_keys file

I created this script to run in order to add to multiple servers:

#!/bin/sh
#Variables
DOMAIN=example.com
SSH-COPY-ID=$(which ssh-copy-id)
# Array to add .ssh authorized keys to systems for access
Systems=(server1 server2 server3 server4 server 5 …)
for S in “${Systems[@]}”
do
#echo $S.$DOMAIN
$SSH-COPY-ID -i ~/.ssh/id_rsa.pub username@$S.$DOMAIN
done

Note: You need to run this script from your home directory and if you have dsa keys instead of rsa keys, make sure to use the correct name.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s