ohmyzsh/plugins/ssh-agent/ssh-agent.plugin.zsh

50 lines
1.4 KiB
Bash
Raw Normal View History

local _plugin__ssh_env
2011-06-05 03:07:55 +02:00
local _plugin__forwarding
2011-06-05 03:07:55 +02:00
function _plugin__start_agent()
{
local -a identities
local lifetime
zstyle -s :omz:plugins:ssh-agent lifetime lifetime
# start ssh-agent and setup environment
ssh-agent ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! ${_plugin__ssh_env}
2011-06-05 03:07:55 +02:00
chmod 600 ${_plugin__ssh_env}
. ${_plugin__ssh_env} > /dev/null
# load identies
zstyle -a :omz:plugins:ssh-agent identities identities
echo starting ssh-agent...
ssh-add $HOME/.ssh/${^identities}
}
# 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
2011-06-05 03:07:55 +02:00
# test if agent-forwarding is enabled
zstyle -b :omz:plugins:ssh-agent agent-forwarding _plugin__forwarding
if [[ ${_plugin__forwarding} == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
# Add a nifty symlink for screen/tmux if agent forwarding
2011-06-05 03:07:55 +02:00
[[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USER-screen
2011-06-05 03:07:55 +02:00
elif [ -f "${_plugin__ssh_env}" ]; then
# Source SSH settings, if applicable
. ${_plugin__ssh_env} > /dev/null
2013-11-05 00:40:13 +01:00
ps x | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null || {
2011-06-05 03:07:55 +02:00
_plugin__start_agent;
}
else
2011-06-05 03:07:55 +02:00
_plugin__start_agent;
fi
2011-06-05 03:07:55 +02:00
# tidy up after ourselves
unfunction _plugin__start_agent
unset _plugin__forwarding
unset _plugin__ssh_env