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