Add key to remote machine
From FBSD_tips
Back to "Admin building blocks"
DRAFT - INCOMPLETE - DRAFT - INCOMPLETE
Here is a shell script that I keep around to add a public key to a remote machine (so it doesn't prompt you for a password on ssh login. It does not make the keys on the local machine secure, so a security breach on this machine will also breach the account on the remote machine. A description of the options processing is here.
You can fetch this script with this command, assuming you have curl installed :
curl --output key_machine.sh http://bsdtips.utcorp.net/mediawiki/index.php\?title=Key_machine.sh\&action=raw
Or view it here key_machine.sh
#!/bin/sh
args=`getopt nh:u: $*`
if [ $? -ne 0 ]
then
echo 'Usage: ...'
echo '-h host to key'
echo '-u usename to key on host'
echo '-n no op'
exit 2
fi
set -- $args
for i
do
case "$i"
in
-n)
NOOP=YES
shift;;
-h)
HOST=$2
shift; shift;;
-u)
UNAME=$2
shift; shift;;
--)
shift
break;;
esac
done
if [ X${HOST} = X ]; then echo "HOST not set"; exit; else echo "HOST is $HOST"; fi
if [ X${UNAME} = X ]; then echo "using current login"; UNAME=${USER}; else echo "UNAME is ${UNAME}"; fi
#
# make sure the keys are where we expect them
#
if [ -e ~/.ssh/id_rsa.pub ]
then
echo "using ~/.ssh/id_rsa.pub"
else
echo "creating ~/.ssh/id_rsa.pub"
ssh-keygen -t rsa -f ~/.ssh/id_rsa
fi
RSAKEY=`cat ~/.ssh/id_rsa.pub`
#
# send the pub key up, make sure permissions are correct
#
echo "you will be prompted for the remote password 3 times"
ssh ${UNAME}@${HOST} mkdir .ssh
ssh ${UNAME}@${HOST} "echo $RSAKEY >> .ssh/authorized_keys"
ssh ${UNAME}@${HOST} chmod 400 .ssh/authorized_keys
