November 28, 2004

SSH - ssh_login - Enter a password once please..

I do a lot of pushing files around. I rely on ssh passwordless authentication for 99% of all of my activites like rsync, CVS, and of course scp. I wanted a way to be able to quickly ensure that I had a passwordless connection first before I fired off 30 rsyncs. So I wrote this little shell function..

Here it is: ssh_login

function ssh_login () {
# This function will get a passwordless authentication session setup for you.
# It is a resuble function in that you can use it at the top of any script
# prior to any ssh'ing to ensure you can do it passwordless. The goal was to
# simply have the user enter his password once.
if [ ! $1 ] ; then
print "Dork, I need an destination argument"
return 255
else
if [ ! -e ~/.ssh/id_rsa -o ! -e ~/.ssh/id_rsa.pub ] ; then
print "Dork, You need to make your keys.."
print "ssh-keygen -t rsa -b 1024 -f ~/.ssh/id_rsa -C \"My comment\""
return 254
fi
# print "Running with comments"
ssh -o RSAAuthentication=yes -o PasswordAuthentication=no \
-o kbdinteractiveauthentication=no -o challengeresponseauthentication=no \
-o StrictHostKeyChecking=no -o CheckHostIP=no $1 date 2>&1 > /dev/null
if [ $? -ne 0 ] ; then
# So we can't get there - let's fix the problem..
# print "Unable to use pawwordless authentication - pushing key now"
cat ${HOME}/.ssh/id_rsa.pub | ssh $1 \
'if [ ! -d ~/.ssh ] ; then
mkdir ~/.ssh/
fi
chmod 700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
cat >> ~/.ssh/authorized_keys'

if [ $? -ne 0 ] ; then
print "Fatal error - must do this manually.."
return 1
else
# print "Pushed key successfull - testing function"
$0 $1 $2
fi
else
# We are good to go..
# print "Success"
return 0
fi
fi
}

So now if I have another function or script that does something like..

function push_dotfiles(){
rsync -avz --recursive -e ssh ~/.profile $1:~/
rsync -avz --recursive -e ssh ~/.zshenv $1:~/
rsync -avz --recursive -e ssh ~/.zshrc $1:~/
.
.
.
rsync -avz --recursive -e ssh ~/.cds/ $1:~/.cds/
}

I would modify it so it does this first..

function push_dotfiles(){
ssh_login $1
rsync -avz --recursive -e ssh ~/.profile $1:~/
rsync -avz --recursive -e ssh ~/.zshenv $1:~/
rsync -avz --recursive -e ssh ~/.zshrc $1:~/
.
.
.
rsync -avz --recursive -e ssh ~/.cds/ $1:~/.cds/
}

Have fun!!

Posted by Steven at November 28, 2004 08:28 PM
Comments
Post a comment









Remember personal info?




You must enter the security code to post!