Browse code

Merge pull request #404 from gwjo/ssh-agent

ssh-agent improvements

Robby Russell authored on 18/07/2011 at 05:03:39
Showing 1 changed files
... ...
@@ -1,23 +1,62 @@
1
-# Based on code from Joseph M. Reagle
2
-# http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
1
+#
2
+# INSTRUCTIONS
3
+#
4
+#   To enabled agent forwarding support add the following to
5
+#   your .zshrc file:
6
+#
7
+#     zstyle :omz:plugins:ssh-agent agent-forwarding on
8
+#
9
+#   To load multiple identies use the identities style, For
10
+#   example:
11
+#
12
+#     zstyle :omz:plugins:ssh-agent id_rsa id_rsa2 id_github
13
+#
14
+#
15
+# CREDITS
16
+#
17
+#   Based on code from Joseph M. Reagle
18
+#   http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
19
+#
20
+#   Agent forwarding support based on ideas from
21
+#   Florent Thoumie and Jonas Pfenniger
22
+#
3 23
 
4
-local SSH_ENV=$HOME/.ssh/environment-$HOST
24
+local _plugin__ssh_env=$HOME/.ssh/environment-$HOST
25
+local _plugin__forwarding
5 26
 
6
-function start_agent {
7
-  /usr/bin/env ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
8
-  chmod 600 ${SSH_ENV}
9
-  . ${SSH_ENV} > /dev/null
10
-  /usr/bin/ssh-add;
27
+function _plugin__start_agent()
28
+{
29
+  local -a identities
30
+
31
+  # start ssh-agent and setup environment
32
+  /usr/bin/env ssh-agent | sed 's/^echo/#echo/' > ${_plugin__ssh_env}
33
+  chmod 600 ${_plugin__ssh_env}
34
+  . ${_plugin__ssh_env} > /dev/null
35
+
36
+  # load identies
37
+  zstyle -a :omz:plugins:ssh-agent identities identities 
38
+  echo starting...
39
+  /usr/bin/ssh-add $HOME/.ssh/${^identities}
11 40
 }
12 41
 
13
-# Source SSH settings, if applicable
42
+# test if agent-forwarding is enabled
43
+zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
44
+if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
45
+  # Add a nifty symlink for screen/tmux if agent forwarding
46
+  [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
14 47
 
15
-if [ -f "${SSH_ENV}" ]; then
16
-  . ${SSH_ENV} > /dev/null
48
+elif [ -f "${_plugin__ssh_env}" ]; then
49
+  # Source SSH settings, if applicable
50
+  . ${_plugin__ssh_env} > /dev/null
17 51
   ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
18
-    start_agent;
52
+    _plugin__start_agent;
19 53
   }
20 54
 else
21
-  start_agent;
55
+  _plugin__start_agent;
22 56
 fi
23 57
 
58
+# tidy up after ourselves
59
+unfunction _plugin__start_agent
60
+unset _plugin__forwarding
61
+unset _plugin__ssh_env
62
+