#!/bin/bash # Script to activate public key ssh access if test -z "$1" ; then echo "Usage:" echo "mkpbkeyaccess remoteserver [remoteusername] [remoteport]" exit 1 fi if test -z "$2" ; then RUSER=$USER else RUSER=$2 fi if test -z "$3" ; then RPORT=22 else RPORT=$3 fi echo "Configure Public key authentification" echo -n "Local user: " echo $USER echo -n "Remote user: " echo $RUSER echo -n "Server: " echo $1 echo -n "Server port:" echo $RPORT echo "Hit any key to proceed, STRG-C to abort" read # Remove ssh Version 1 files rm -f $HOME/.ssh/known_hosts rm -f $HOME/.ssh/identity* if test ! -e "$HOME/.ssh/id_dsa"; then echo "Generating new dsa key" ssh-keygen -t dsa -q -N "" -f $HOME/.ssh/id_dsa fi if test ! -e "$HOME/.ssh/id_rsa"; then echo "Generating new rsa key" ssh-keygen -t rsa -q -N "" -f $HOME/.ssh/id_rsa fi # Add these keys to local authorization (for ssh localhost) cat $HOME/.ssh/id*.pub >> $HOME/.ssh/authorized_keys2 # Add the keys to the remote servers list echo "**************************************************************************" echo "You will now be asked three times for your (remote) password." echo "This is OK and will hopefully be the last time" echo "Ignore any file not found/ directory existing messages" echo "and answer a possible warning with yes" echo "**************************************************************************" # Just for sure, ensure ~/.ssh exists ssh -p $RPORT -l $RUSER $1 mkdir .ssh scp -P $RPORT $RUSER@$1:.ssh/authorized_keys2 /tmp/authorized_keys2.$USER cat $HOME/.ssh/id*.pub >> /tmp/authorized_keys2.$USER scp -P $RPORT /tmp/authorized_keys2.$USER $RUSER@$1:.ssh/authorized_keys2 echo "**************************************************************************" echo "finished, cleaning up.." rm /tmp/authorized_keys2.$USER