This script is to save my time when installing new servers. Sure, I could just write out the last line in the script but its more effort than this and there's too likely to be a mistake. This script will prompt for a host, username (default is your current username), and then will look in the standard spot (~/.ssh/) for id_dsa.pub or id_rsa.pub. You can also specify a filename.
Basic Syntax:
./install_public_key.sh
"Advanced" Syntax:
./install_public_key.sh /path/to/my/key.pub
The script: (download)
#!/bin/bash
if [ "`echo -n`" = "-n" ]; then
n=""; c="\c"
else
n="-n"; c=""
fi
echo $n "Host: $c"; read host
if [ "$host" = "" ]; then
echo "Exiting: Host not specified"
exit 1
fi
echo $n "Username ("`whoami`"):$c"; read user
if [ "$user" = "" ]; then
user=`whoami`
fi
DSA_FILE=~/.ssh/id_dsa.pub
RSA_FILE=~/.ssh/id_rsa.pub
if [ -f "$1" ]; then
KEYFILE=$1
elif [ -f $DSA_FILE ]; then
KEYFILE=$DSA_FILE
elif [ -f $RSA_FILE ]; then
KEYFILE=$RSA_FILE
else
echo "Exiting: Public key not found"
exit 1
fi
PUBKEY=`cat $KEYFILE`
ssh $user@$host "mkdir -p ~/.ssh;echo \"$PUBKEY\" >> ~/.ssh/authorized_keys;chmod go-w ~/.ssh;chmod go-w ~/.ssh/authorized_keys"
Comments
Anonymous (not verified)
Sat, 11/27/2010 - 04:39
Permalink
Where Can I use this Script?
Where Can I use this Script? I'm not likely familiar with Shell Script.
Ruby,
van">http://www.van-insurance-cheap.co.uk/">van insurance quote