]> git.rmz.io Git - dotfiles.git/blob - zsh/plugins/ssh-agent/ssh-agent.plugin.zsh
Squashed commit of the following + cleanup afterwards:
[dotfiles.git] / zsh / plugins / ssh-agent / ssh-agent.plugin.zsh
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 #
23
24 local _plugin__ssh_env=$HOME/.ssh/environment-$HOST
25 local _plugin__forwarding
26
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}
40 }
41
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
47
48 elif [ -f "${_plugin__ssh_env}" ]; then
49 # Source SSH settings, if applicable
50 . ${_plugin__ssh_env} > /dev/null
51 ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
52 _plugin__start_agent;
53 }
54 else
55 _plugin__start_agent;
56 fi
57
58 # tidy up after ourselves
59 unfunction _plugin__start_agent
60 unset _plugin__forwarding
61 unset _plugin__ssh_env
62