It also
- creates public key locally, if not yet generated;
- checks if public host is already there, and don't make a dup.
Must be executed on client side! It will copy public key to the server and concatenate with the authorized_keys file.
#!/bin/ksh
RUSER=someuser
VSA=someserver.com
L="$RUSER@$VSA"
OPTS="-o StrictHostKeyChecking=no" #-v
if [ ! -e ~/.ssh/id_rsa.pub ]; then
echo "========= Generating public key, with empty passpharase..."
ssh-keygen -q -f ~/.ssh/id_rsa -t rsa
#client permissions
chmod 700 ~/.ssh
chmod go-rwx ~/.ssh/*
fi
echo "========= Copy pubkey for the $L..."
scp $OPTS ~/.ssh/id_rsa.pub $L: 2>/dev/null
if [ $? == "0" ]; then
echo "Done."
fi
echo "========= Setup the public key for $L and permissions there..."
ssh $OPTS $L '
if [ ! -e ~/.ssh ]; then mkdir ~/.ssh; chmod 700 ~/.ssh; fi;
cd ~/.ssh;
grep -q -s `head -1 ~/id_rsa.pub | cut -d " " -f 2` authorized_keys;
if [ $? != 0 ]; then cat ~/id_rsa.pub >> authorized_keys; echo "Key added";
else echo "Key was already there"; fi;
chmod 600 authorized_keys;
rm ~/id_rsa.pub
' 2>/dev/null
Enter empty passphrase when asked during ssh key generation.
Tested on AIX, but should run perfect (ksh) on other systems without modifications.

1 comment:
Its wonderful.
I was writing a perl script which will do interactive shell based installation.
It will help
Post a Comment