ssh-agent.plugin.zsh 1.17 KB
Newer Older
1
typeset _agent_forwarding _ssh_env_cache
gwjo's avatar
gwjo committed
2

3
4
5
function _start_agent() {
	local lifetime
	local -a identities
6

7
8
	# start ssh-agent and setup environment
	zstyle -s :omz:plugins:ssh-agent lifetime lifetime
9

10
	ssh-agent -s ${lifetime:+-t} ${lifetime} | sed 's/^echo/#echo/' >! $_ssh_env_cache
11
12
	chmod 600 $_ssh_env_cache
	. $_ssh_env_cache > /dev/null
13

14
15
16
17
18
	# load identies
	zstyle -a :omz:plugins:ssh-agent identities identities

	echo starting ssh-agent...
	ssh-add $HOME/.ssh/${^identities}
gwjo's avatar
gwjo committed
19
20
}

21
# Get the filename to store/lookup the environment from
22
_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
23

gwjo's avatar
gwjo committed
24
# test if agent-forwarding is enabled
25
26
27
28
29
30
31
32
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
33
	ps x | grep ssh-agent | grep -q $SSH_AGENT_PID || {
34
35
		_start_agent
	}
gwjo's avatar
gwjo committed
36
else
37
	_start_agent
gwjo's avatar
gwjo committed
38
39
fi

gwjo's avatar
gwjo committed
40
# tidy up after ourselves
41
42
unset _agent_forwarding _ssh_env_cache
unfunction _start_agent