From 61ebb4de32d390c7ed6fdc8a0823ec6024795fc9 Mon Sep 17 00:00:00 2001
From: Moritz Beber <m.beber@jacobs-university.de>
Date: Wed, 11 Aug 2010 16:12:34 +0200
Subject: Removed inconsistencies.

The automatic launching now first checks for an already running agent before it
launches its own one. Launching is now consistent across windows due to the use
of SSH_ENV again. The return code of ssh-add is now specifically checked for
returncode 2 which indicates that the socket is broken.
---
 ...2009-09-03-working-with-key-passphrases.textile | 32 +++++++++++++++++-----
 1 file changed, 25 insertions(+), 7 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 604221e..12f74b6 100644
--- a/_posts/2009-09-03-working-with-key-passphrases.textile
+++ b/_posts/2009-09-03-working-with-key-passphrases.textile
@@ -36,31 +36,49 @@ 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>
-# automatically start the ssh-agent
+SSH_ENV="$HOME/.ssh/environment"
+
+# start the ssh-agent
 function start_agent {
     echo "Initializing new SSH agent..."
     # spawn ssh-agent
-    eval `ssh-agent` > /dev/null
+    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 with proper $SSH_AGENT_PID
 ps -ef | grep $SSH_AGENT_PID | grep -v grep > /dev/null
 if [ $? -eq 0 ]; then
-# test whether standard identities have been added to the agent already
+    # test whether standard identities have been added to the agent already
     ssh-add -l | grep "The agent has no identities" > /dev/null
     if [ $? -eq 0 ]; then
         ssh-add
         # $SSH_AUTH_SOCK broken so we start a new proper agent
-        if [ $? -ne 0 ];then
+        if [ $? -eq 2 ];then
             start_agent
         fi
     fi 
-# if $SSH_AGENT_PID is not properly set, the socket is probably neither better
-# to start a new agent
+# if $SSH_AGENT_PID is not properly set, we might be able to load one from
+# $SSH_ENV
 else
-    start_agent
+    . $SSH_ENV > /dev/null
+    ps -ef | grep $SSH_AGENT_PID | grep -v grep > /dev/null
+    if [ $? -eq 0 ]; then
+        # test whether standard identities have been added to the agent already
+        ssh-add -l | grep "The agent has no identities" > /dev/null
+        if [ $? -eq 0 ]; then
+            ssh-add
+            # $SSH_AUTH_SOCK broken so we start a new proper agent
+            if [ $? -eq 2 ];then
+                start_agent
+            fi
+    fi 
+    else
+        start_agent
+    fi
 fi
 </pre>
 
-- 
cgit v1.3.1

