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


From 3aedad7490335796eb80bd549b9f526a699f374d Mon Sep 17 00:00:00 2001
From: Moritz Beber <m.beber@jacobs-university.de>
Date: Wed, 11 Aug 2010 14:21:11 +0200
Subject: fixed bug with SSH_AGENT_PID

The variable SSH_AGENT_PID might still be set, so it should not be used for
testing. Resorted to a ps test instead.
---
 _posts/2009-09-03-working-with-key-passphrases.textile | 5 +++--
 1 file changed, 3 insertions(+), 2 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 978b9f2..11fd037 100644
--- a/_posts/2009-09-03-working-with-key-passphrases.textile
+++ b/_posts/2009-09-03-working-with-key-passphrases.textile
@@ -47,8 +47,9 @@ function start_agent {
     ssh-add
 }   
 
-# check for running ssh-agent 
-if [ ${SSH_AGENT_PID} ]; then
+# check for running ssh-agent
+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 "agent has no identities"
-- 
cgit v1.3.1


From eedf0d0871a158c4254515c09ec2eca7b43a32dc Mon Sep 17 00:00:00 2001
From: Moritz Beber <m.beber@jacobs-university.de>
Date: Wed, 11 Aug 2010 15:22:17 +0200
Subject: Straightened out some flaws in the behaviour.

The idea is to have a single ssh-agent instance that holds all the keys.
Checking and spawning of ssh-agents should be consistent now.
---
 ...2009-09-03-working-with-key-passphrases.textile | 23 ++++++++++++----------
 1 file changed, 13 insertions(+), 10 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 11fd037..604221e 100644
--- a/_posts/2009-09-03-working-with-key-passphrases.textile
+++ b/_posts/2009-09-03-working-with-key-passphrases.textile
@@ -36,29 +36,32 @@ 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"
 # automatically start the ssh-agent
 function start_agent {
     echo "Initializing new SSH agent..."
-    ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
+    # spawn ssh-agent
+    eval `ssh-agent` > /dev/null
     echo succeeded
-    chmod 600 "${SSH_ENV}"
-    . "${SSH_ENV}" > /dev/null
     ssh-add
 }   
 
-# check for running ssh-agent
-ps -ef | grep ${SSH_AGENT_PID} | grep -v grep > /dev/null
+# 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
-    ssh-add -l | grep "agent has no identities"
+# 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
+            start_agent
+        fi
     fi 
+# if $SSH_AGENT_PID is not properly set, the socket is probably neither better
+# to start a new agent
 else
     start_agent
-fi  
+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


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


From 0ea4d26b9230a38ab43eaeb95eddeff08bddd8b0 Mon Sep 17 00:00:00 2001
From: Moritz Beber <m.beber@jacobs-university.de>
Date: Wed, 11 Aug 2010 16:25:21 +0200
Subject: Removed some code redundancies.

---
 ...2009-09-03-working-with-key-passphrases.textile | 23 ++++++++++------------
 1 file changed, 10 insertions(+), 13 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 12f74b6..13a2f92 100644
--- a/_posts/2009-09-03-working-with-key-passphrases.textile
+++ b/_posts/2009-09-03-working-with-key-passphrases.textile
@@ -47,11 +47,10 @@ function start_agent {
     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 for identities
+function test_identities {
     # 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
@@ -61,21 +60,19 @@ if [ $? -eq 0 ]; then
             start_agent
         fi
     fi 
+}
+
+# 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_identities
 # if $SSH_AGENT_PID is not properly set, we might be able to load one from
 # $SSH_ENV
 else
     . $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 
+        test_identities
     else
         start_agent
     fi
-- 
cgit v1.3.1


From 8aacd9ef928015dc34c0a1a3c66eab53741974d3 Mon Sep 17 00:00:00 2001
From: Moritz Beber <m.beber@jacobs-university.de>
Date: Mon, 16 Aug 2010 16:02:26 +0200
Subject: Revised grep check

Changed the grep check from simply ignoring its own entry, i.e., grep -v grep to
checking that the process running under the SSH_AGENT_PID is actually an
ssh-agent.
---
 _posts/2009-09-03-working-with-key-passphrases.textile | 4 ++--
 1 file changed, 2 insertions(+), 2 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 13a2f92..19e45bd 100644
--- a/_posts/2009-09-03-working-with-key-passphrases.textile
+++ b/_posts/2009-09-03-working-with-key-passphrases.textile
@@ -63,14 +63,14 @@ function test_identities {
 }
 
 # check for running ssh-agent with proper $SSH_AGENT_PID
-ps -ef | grep $SSH_AGENT_PID | grep -v grep > /dev/null
+ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null
 if [ $? -eq 0 ]; then
     test_identities
 # if $SSH_AGENT_PID is not properly set, we might be able to load one from
 # $SSH_ENV
 else
     . $SSH_ENV > /dev/null
-    ps -ef | grep $SSH_AGENT_PID | grep -v grep > /dev/null
+    ps -ef | grep $SSH_AGENT_PID | grep ssh-agent > /dev/null
     if [ $? -eq 0 ]; then
         test_identities
     else
-- 
cgit v1.3.1

