#!/bin/bash #usage: ssh-setup-keys [-a] # #-a : check for automated logins and configure as necessary TMP=`getopt --name="$0" -a automate -o a -- "$@"` eval set -- "$TMP" until [ "$1" == "--" ]; do case "$1" in --automate|-a) AUTOMATE=1;; esac shift done shift #first, check to see if the local account already has a public/private key echo -n "Checking on public/private key... " if [ ! -f $HOME/.ssh/id_rsa ]; then echo "no key found, generating..." ssh-keygen -t rsa -b 4096 else echo "key already exists, moving on..." fi #now disseminate the key echo -n "Enter the host name or IP of the host you wish to authenticate to: " read REMOTE_HOST echo -n "Enter the remote username you wish to authenticate to: " read REMOTE_USER echo "You will now authenticate to the machine in order to disseminate the public key." cat $HOME/.ssh/id_rsa.pub | ssh $REMOTE_USER@$REMOTE_HOST 'cat - >> ~/.ssh/authorized_keys' if [ x"$AUTOMATE" == x"1" ]; then echo -n "Checking to see whether automated logins are configured... " #check to see if the agent script has been added to the users if [ x`grep SSHAGENT $HOME/.bashrc` == x'' ]; then echo -n "they are not; adding... " echo "" >> $HOME/.bashrc #in case there's no newline #credit to Mark A. Hershberger for following bit of code #http://mah.everybody.org/docs/ssh cat <<'EOF' >> $HOME/.bashrc SSHAGENT=/usr/bin/ssh-agent SSHAGENTARGS="-s" if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then eval `$SSHAGENT $SSHAGENTARGS` trap "kill $SSH_AGENT_PID" 0 fi EOF echo "done." else echo "they are." fi fi