2016-08-21 07:08:34 +08:00
|
|
|
typeset _agent_forwarding _ssh_env_cache
|
2010-09-25 08:46:52 +08:00
|
|
|
|
2016-08-21 07:08:34 +08:00
|
|
|
function _start_agent() {
|
|
|
|
local lifetime
|
|
|
|
local -a identities
|
2011-06-05 10:16:10 +08:00
|
|
|
|
2016-08-21 07:08:34 +08:00
|
|
|
# start ssh-agent and setup environment
|
|
|
|
zstyle -s :omz:plugins:ssh-agent lifetime lifetime
|
2011-06-05 10:16:10 +08:00
|
|
|
|
2014-09-30 17:33:42 +08:00
|
|
|
ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
|
2016-08-21 07:08:34 +08:00
|
|
|
chmod 600 $_ssh_env_cache
|
|
|
|
. $_ssh_env_cache > /dev/null
|
2013-03-16 12:06:46 +08:00
|
|
|
|
2016-08-21 07:08:34 +08:00
|
|
|
# load identies
|
|
|
|
zstyle -a :omz:plugins:ssh-agent identities identities
|
|
|
|
|
|
|
|
echo starting ssh-agent...
|
|
|
|
ssh-add $HOME/.ssh/${^identities}
|
2010-09-25 08:46:52 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 02:44:58 +08:00
|
|
|
# Get the filename to store/lookup the environment from
|
2016-08-21 07:08:34 +08:00
|
|
|
_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
|
2014-03-14 02:44:58 +08:00
|
|
|
|
2011-06-05 09:07:55 +08:00
|
|
|
# test if agent-forwarding is enabled
|
2016-08-21 07:08:34 +08:00
|
|
|
zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding
|
|
|
|
|
|
|
|
if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
|
|
|
|
# Add a nifty symlink for screen/tmux if agent forwarding
|
|
|
|
[[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
|
|
|
|
elif [[ -f "$_ssh_env_cache" ]]; then
|
|
|
|
# Source SSH settings, if applicable
|
|
|
|
. $_ssh_env_cache > /dev/null
|
2016-09-17 19:01:10 +08:00
|
|
|
ps x | grep ssh-agent | grep -q $SSH_AGENT_PID || {
|
2016-08-21 07:08:34 +08:00
|
|
|
_start_agent
|
|
|
|
}
|
2010-09-25 08:46:52 +08:00
|
|
|
else
|
2016-08-21 07:08:34 +08:00
|
|
|
_start_agent
|
2010-09-25 08:46:52 +08:00
|
|
|
fi
|
|
|
|
|
2011-06-05 09:07:55 +08:00
|
|
|
# tidy up after ourselves
|
2016-08-21 07:08:34 +08:00
|
|
|
unset _agent_forwarding _ssh_env_cache
|
|
|
|
unfunction _start_agent
|