ssh-agent.plugin.zsh 2.2 KB
Newer Older
gwjo's avatar
gwjo committed
1
2
3
4
5
6
7
8
#
# INSTRUCTIONS
#
#   To enabled agent forwarding support add the following to
#   your .zshrc file:
#
#     zstyle :omz:plugins:ssh-agent agent-forwarding on
#
9
#   To load multiple identities use the identities style, For
10
11
#   example:
#
12
#     zstyle :omz:plugins:ssh-agent identities id_rsa id_rsa2 id_github
13
#
14
15
16
17
18
19
#   To set the maximum lifetime of the identities, use the
#   lifetime style. The lifetime may be specified in seconds
#   or as described in sshd_config(5) (see TIME FORMATS)
#   If left unspecified, the default lifetime is forever.
#
#     zstyle :omz:plugins:ssh-agent lifetime 4h
gwjo's avatar
gwjo committed
20
21
22
23
24
25
26
27
28
#
# CREDITS
#
#   Based on code from Joseph M. Reagle
#   http://www.cygwin.com/ml/cygwin/2001-06/msg00537.html
#
#   Agent forwarding support based on ideas from
#   Florent Thoumie and Jonas Pfenniger
#
gwjo's avatar
gwjo committed
29

30
local _plugin__ssh_env
gwjo's avatar
gwjo committed
31
local _plugin__forwarding
gwjo's avatar
gwjo committed
32

gwjo's avatar
gwjo committed
33
34
function _plugin__start_agent()
{
35
  local -a identities
36
37
  local lifetime
  zstyle -s :omz:plugins:ssh-agent lifetime lifetime
38
39

  # start ssh-agent and setup environment
40
  /usr/bin/env ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' > ${_plugin__ssh_env}
gwjo's avatar
gwjo committed
41
42
  chmod 600 ${_plugin__ssh_env}
  . ${_plugin__ssh_env} > /dev/null
43
44

  # load identies
45
  zstyle -a :omz:plugins:ssh-agent identities identities
46
47
  echo starting ssh-agent...

48
  /usr/bin/ssh-add $HOME/.ssh/${^identities}
gwjo's avatar
gwjo committed
49
50
}

51
52
53
54
55
56
57
58
# Get the filename to store/lookup the environment from
if (( $+commands[scutil] )); then
  # It's OS X!
  _plugin__ssh_env="$HOME/.ssh/environment-$(scutil --get ComputerName)"
else
  _plugin__ssh_env="$HOME/.ssh/environment-$HOST"
fi

gwjo's avatar
gwjo committed
59
60
# test if agent-forwarding is enabled
zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
61
62
if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
  # Add a nifty symlink for screen/tmux if agent forwarding
gwjo's avatar
gwjo committed
63
  [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
gwjo's avatar
gwjo committed
64

gwjo's avatar
gwjo committed
65
66
67
elif [ -f "${_plugin__ssh_env}" ]; then
  # Source SSH settings, if applicable
  . ${_plugin__ssh_env} > /dev/null
68
  ps x | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null || {
gwjo's avatar
gwjo committed
69
    _plugin__start_agent;
70
  }
gwjo's avatar
gwjo committed
71
else
gwjo's avatar
gwjo committed
72
  _plugin__start_agent;
gwjo's avatar
gwjo committed
73
74
fi

gwjo's avatar
gwjo committed
75
76
77
78
79
# tidy up after ourselves
unfunction _plugin__start_agent
unset _plugin__forwarding
unset _plugin__ssh_env