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