From 121b3c67c362acf87d646631801b3c77a2e785e1 Mon Sep 17 00:00:00 2001
From: Moritz Beber <m.beber@jacobs-university.de>
Date: Wed, 11 Aug 2010 14:09:32 +0200
Subject: more careful ssh-agent starting

I modified the script for automatic ssh-agent starting to only start a new agent
if one isn't running already and to only add the keys from standard file
    locations only if the agent doesn't already contain any identities.
The reasoning for me was that every time I open a new bash window or fire up
'screen', I would be asked for the passphrase.
---
 ...2009-09-03-working-with-key-passphrases.textile | 41 ++++++++++++----------
 1 file changed, 22 insertions(+), 19 deletions(-)

(limited to '_posts/2009-09-03-working-with-key-passphrases.textile')

diff --git a/_posts/2009-09-03-working-with-key-passphrases.textile b/_posts/2009-09-03-working-with-key-passphrases.textile
index 282e8be..978b9f2 100644
--- a/_posts/2009-09-03-working-with-key-passphrases.textile
+++ b/_posts/2009-09-03-working-with-key-passphrases.textile
@@ -35,27 +35,30 @@ h2. Auto-launching ssh-agent on msysgit
 
 You can run @ssh-agent@ automatically when you open bash by adding the following to your @~/.profile@ or @~/.bashrc@ file:
 
-<pre>SSH_ENV="$HOME/.ssh/environment"
-
+<pre>
+SSH_ENV="$HOME/.ssh/environment"
+# automatically start the ssh-agent
 function start_agent {
-  echo "Initializing new SSH agent..."
-  /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
-  echo succeeded
-  chmod 600 "${SSH_ENV}"
-  . "${SSH_ENV}" > /dev/null
-  /usr/bin/ssh-add;
-}
-
-# Source SSH settings, if applicable
-if [ -f "${SSH_ENV}" ]; then
-  . "${SSH_ENV}" > /dev/null
-  #ps ${SSH_AGENT_PID} doesn't work under cywgin
-  ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
-    start_agent;
-  }
+    echo "Initializing new SSH agent..."
+    ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
+    echo succeeded
+    chmod 600 "${SSH_ENV}"
+    . "${SSH_ENV}" > /dev/null
+    ssh-add
+}   
+
+# check for running ssh-agent 
+if [ ${SSH_AGENT_PID} ]; then
+    # test whether standard identities have been added to
+    # the agent already
+    ssh-add -l | grep "agent has no identities"
+    if [ $? -eq 0 ]; then
+        ssh-add
+    fi 
 else
-  start_agent;
-fi</pre>
+    start_agent
+fi  
+</pre>
 
 p(. *Note:* If you don't use the default key names, or store your keys in a different path, you will need to add the path to the @/usr/bin/ssh-add@ line so that ssh knows where to find your key.
 
-- 
cgit v1.3.1

