Enter your ssh-passphrase once, use it many times, even from crontab

Here is a way to use ssh-agent, ssh-add without having to type you password every session. Just once (when the machine has been rebooted) you will have to enter you passphrase, from then on you are authenticated. Here is the script you will have to add to your .bashrc or .profile in you homedirectory:

variables=~/.ssh/variables

sshadd() {
source "$variables" > /dev/null
ssh-add -l > /dev/null 2>&1
case "$?" in
  1)
   ssh-add > /dev/null 2>&1
  ;;
  2)
   rm "$variables"
   sshagent
  ;;
esac
}

sshagent() {
if [ -f "$variables" ] ; then
  sshadd
else
  ssh-agent -s > $variables
  sshadd
fi
}

sshagent

Have fun, I have been using this great hint for quite some time now, it seems to work perfectly!

Comments

some pollish

some pollish ;-)
---------------------------------------------------------
:
#!/bin/sh
#from http://meinit.nl/enter-your-ssh-passphrase-once-use-it-many-times-even-f...
set -x
RCSRev=`sh -c "echo '$Revision: 1.2 $' | cut -f2 -d' ' "`
VER=$RCSRev
PN=`basename "$0"`
set -x
#not work# umask 0177
variables="$HOME"/.ssh/"$PN"_variables

sshadd() {
set -x
. "$variables" > /dev/null
ssh-add -l > /dev/null 2>&1
case "$?" in
1)
ssh-add > /dev/null 2>&1
;;
2)
rm "$variables"
sshagent
;;
esac
}

sshagent() {
set -x
if [ -f "$variables" ] ; then
sshadd
else
ssh-agent -s > "$variables"
sshadd
fi
}

sshagent
---------------------------------------------------------

and just do make it save ;-)

#---------------------------------- cut here ----------------------------------
# This is a shell archive. Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Wolfgang Anger on Thu Dec 13 15:11:24 2007
#
# This archive contains:
# ssh_agent_start
#
# Modification/access file times will be preserved.
# Error checking via sum(1) will be performed.

LANG=""; export LANG
PATH=/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:$PATH; export PATH
EXIT_STATUS=0

if sum -r /dev/null 2>&1
then
sumopt='-r'
else
sumopt=''
fi

echo x - ssh_agent_start
sed 's/^@//' >ssh_agent_start <<'@EOF'
:
#!/bin/sh
#from http://meinit.nl/enter-your-ssh-passphrase-once-use-it-many-times-even-f...
set -x
RCSRev=`sh -c "echo '$Revision: 1.2 $' | cut -f2 -d' ' "`
VER=$RCSRev
PN=`basename "$0"`
set -x
#not work# umask 0177
variables="$HOME"/.ssh/"$PN"_variables

sshadd() {
set -x
@. "$variables" > /dev/null
ssh-add -l > /dev/null 2>&1
case "$?" in
1)
ssh-add > /dev/null 2>&1
;;
2)
rm "$variables"
sshagent
;;
esac
}

sshagent() {
set -x
if [ -f "$variables" ] ; then
sshadd
else
ssh-agent -s > "$variables"
sshadd
fi
}

sshagent
@EOF
set `sum $sumopt

You should probably also

You should probably also check out the "keychain" package, created by Gentoo.
It supports the above, but goes even further.

http://www.gentoo.org/proj/en/keychain/

Nice script. :) I've been

Nice script. :)

I've been looking for something like this. But I've encountered some problems:
On my OpenBSD box:
--
/home/raistlin/bin/ssh_agent_start[26]: source: not found
/home/raistlin/bin/ssh_agent_start[26]: 2>&amp : illegal file descriptor name
/home/raistlin/bin/ssh_agent_start[26]: 1: not found
--
Note: source is definitely installed.

On my FreeBSD box:
--
/home/raistlin/bin/ssh_agent_start: 26: Syntax error: Bad fd number
--

Source is definitely in path on both machines. I'm using Zsh on both boxes.
After running the script the variables-file is created and if I then
execute "source ~/.ssh/variables" and "ssh-add" manually
it works.

Hi Mats, About that 2>&amp

Hi Mats,

About that 2>&amp error, you are right, it was a copy-paste failure and has been updated in the story.

About source, I notice this too on different platform from time to time, replace source with a dot(.).

Regards,

Robert de Bock
[email protected]

About Consultancy Articles Contact




References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning [email protected]