#/bin/sh

# Author:  Edward A Robinson
# Contact: earobinson@gmail.com

# If you would like to connect to your machine through ssh without
# being asked for a password you should do this.
# 1. Create a public/private key pair
#        ssh-keygen -t rsa
#        Please Do not forget not to write any passphrase, just empty
#        for no passphrase for this to work
# 2. Copy te file id_rsa.pub to the $HOME/.ssh directory of the machine
#    you wish to connect to, where $HOME is the direcotory fo the user
#    you would like to connect as. /root/.ssh in the case you would
#    like to connect as root. Consider you would like to connect as the
#    user user
#        scp $HOME/.ssh/id_rsa.pub (line break)
#        usuario@server.remoto:/home/user/.ssh/authorized_keys2
# 3. Now you can connecto the remote server with out being asked for a password.
#        ssh -l usuario server.remoto

# Source: http://www.go2linux.org/node/16

# Verify that an ip is given
if [ $# -ne 1 ]; then
    echo 1>&2 Usage: $0 \<target name/ip\>
    exit 127
fi

# Check for key
if !(test -f ~/.ssh/id_rsa && test -f ~/.ssh/id_rsa.pub) ; then
   echo 'ssh key not found now running "ssh-keygen -t rsa"'

   # Make keys
   ssh-keygen -t rsa

   # Check keys have been made
    if !(test -f ~/.ssh/id_rsa && test -f ~/.ssh/id_rsa.pub) ; then
        echo 1>&2 ssh kets ~/.ssh/id_rsa && and ~/.ssh/id_rsa.pub still could not be found exiting.
        exit 127
    fi
fi

echo Copying keys to target \($1\) please enter targets password. please ignore any mkdir errors.
# Copy key to target
ssh $1 "mkdir ~/.ssh; echo `cat ~/.ssh/id_rsa.pub` >> ~/.ssh/authorized_keys2"
#scp ~/.ssh/id_rsa.pub $1:~/.ssh/authorized_keys2
