ssh-agent.plugin.zsh 1.24 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
34
35
36
37
38
	if [[ $USER == "root" ]]; then
		FILTER="ax"
	else
		FILTER="x"
	fi
	ps $FILTER | grep ssh-agent | grep -q $SSH_AGENT_PID || {
39
40
		_start_agent
	}
gwjo's avatar
gwjo committed
41
else
42
	_start_agent
gwjo's avatar
gwjo committed
43
44
fi

gwjo's avatar
gwjo committed
45
# tidy up after ourselves
46
47
unset _agent_forwarding _ssh_env_cache
unfunction _start_agent