ssh-agent.plugin.zsh 1.39 KB
Newer Older
1
local _plugin__ssh_env
gwjo's avatar
gwjo committed
2
local _plugin__forwarding
gwjo's avatar
gwjo committed
3

gwjo's avatar
gwjo committed
4
5
function _plugin__start_agent()
{
6
  local -a identities
7
8
  local lifetime
  zstyle -s :omz:plugins:ssh-agent lifetime lifetime
9
10

  # start ssh-agent and setup environment
11
  ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! ${_plugin__ssh_env}
gwjo's avatar
gwjo committed
12
13
  chmod 600 ${_plugin__ssh_env}
  . ${_plugin__ssh_env} > /dev/null
14
15

  # load identies
16
  zstyle -a :omz:plugins:ssh-agent identities identities
17
18
  echo starting ssh-agent...

19
  ssh-add $HOME/.ssh/${^identities}
gwjo's avatar
gwjo committed
20
21
}

22
23
24
25
26
27
28
29
# 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
30
31
# test if agent-forwarding is enabled
zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
32
33
if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
  # Add a nifty symlink for screen/tmux if agent forwarding
gwjo's avatar
gwjo committed
34
  [[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
gwjo's avatar
gwjo committed
35

gwjo's avatar
gwjo committed
36
37
38
elif [ -f "${_plugin__ssh_env}" ]; then
  # Source SSH settings, if applicable
  . ${_plugin__ssh_env} > /dev/null
39
  ps x | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null || {
gwjo's avatar
gwjo committed
40
    _plugin__start_agent;
41
  }
gwjo's avatar
gwjo committed
42
else
gwjo's avatar
gwjo committed
43
  _plugin__start_agent;
gwjo's avatar
gwjo committed
44
45
fi

gwjo's avatar
gwjo committed
46
47
48
49
# tidy up after ourselves
unfunction _plugin__start_agent
unset _plugin__forwarding
unset _plugin__ssh_env