2016-08-20 23:08:34 +00:00
|
|
|
typeset _agent_forwarding _ssh_env_cache
|
2010-09-25 00:46:52 +00:00
|
|
|
|
2016-08-20 23:08:34 +00:00
|
|
|
function _start_agent() {
|
|
|
|
local lifetime
|
|
|
|
local -a identities
|
2011-06-05 02:16:10 +00:00
|
|
|
|
2016-08-20 23:08:34 +00:00
|
|
|
# start ssh-agent and setup environment
|
|
|
|
zstyle -s :omz:plugins:ssh-agent lifetime lifetime
|
2011-06-05 02:16:10 +00:00
|
|
|
|
2014-09-30 09:33:42 +00:00
|
|
|
ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
|
2016-08-20 23:08:34 +00:00
|
|
|
chmod 600 $_ssh_env_cache
|
|
|
|
. $_ssh_env_cache > /dev/null
|
2013-03-16 04:06:46 +00:00
|
|
|
|
2016-08-20 23:08:34 +00:00
|
|
|
# load identies
|
|
|
|
zstyle -a :omz:plugins:ssh-agent identities identities
|
|
|
|
|
|
|
|
echo starting ssh-agent...
|
|
|
|
ssh-add $HOME/.ssh/${^identities}
|
2010-09-25 00:46:52 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 18:44:58 +00:00
|
|
|
# Get the filename to store/lookup the environment from
|
2016-08-20 23:08:34 +00:00
|
|
|
_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
|
2014-03-13 18:44:58 +00:00
|
|
|
|
2011-06-05 01:07:55 +00:00
|
|
|
# test if agent-forwarding is enabled
|
2016-08-20 23:08:34 +00: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 11:01:10 +00:00
|
|
|
ps x | grep ssh-agent | grep -q $SSH_AGENT_PID || {
|
2016-08-20 23:08:34 +00:00
|
|
|
_start_agent
|
|
|
|
}
|
2010-09-25 00:46:52 +00:00
|
|
|
else
|
2016-08-20 23:08:34 +00:00
|
|
|
_start_agent
|
2010-09-25 00:46:52 +00:00
|
|
|
fi
|
|
|
|
|
2011-06-05 01:07:55 +00:00
|
|
|
# tidy up after ourselves
|
2016-08-20 23:08:34 +00:00
|
|
|
unset _agent_forwarding _ssh_env_cache
|
|
|
|
unfunction _start_agent
|