SSH Passwordless Login

Here is a script that adds your SSH public key to a remote hosts authorized_keys file. Thus enabling you to login without a password.
(This does require that the remote host allows the use of the authorized_keys feature.)

#!/bin/bash

if [ ! -n "$1" ] || [ ! -n "$2" ]
then
       echo "USAGE: authorise hostname username"
       exit
fi

if [ ! -e ~/.ssh/id_rsa.pub ]
then
        #Create priavte/public key pair if they dont exist
        ssh-keygen -t rsa  -f ~/.ssh/id_rsa -N ""
fi

#Readin public key
PUBKEY=`cat ~/.ssh/id_rsa.pub`

ssh $2@$1 "[ -d ~/.ssh ] || mkdir ~/.ssh; touch ~/.ssh/authorized_keys; echo \""$PUBKEY"\" >> ~/.ssh/authorized_keys"

Download Here

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *