a5d59943ce
based on https://stackoverflow.com/a/48509425/2654518, which is based on http://rabexc.org/posts/pitfalls-of-ssh-agents Current code has a few issues: depends on `SSH_AUTH_SOCK` and `SSH_AGENT_PID` env variables, which might not be available in every shell session; and tries to create a new socket for agent-forwarding by checking `SSH_AUTH_SOCKET` instead of `SSH_AUTH_SOCK`. Also, it's safer to create the env file with 066 flags and in the user home directory.
35 lines
742 B
Bash
35 lines
742 B
Bash
#
|
|
# sets up ssh-agent
|
|
#
|
|
|
|
# don't do anything unless we can actually use ssh-agent
|
|
if (( ! ${+commands[ssh-agent]} )); then
|
|
return 1
|
|
fi
|
|
|
|
ssh-add -l &>/dev/null
|
|
if (( ? == 2 )); then
|
|
# Unable to contact the authentication agent
|
|
|
|
# Load stored agent connection info
|
|
local ssh_env="${HOME}/.ssh-agent"
|
|
[[ -r ${ssh_env} ]] && source ${ssh_env} >/dev/null
|
|
|
|
ssh-add -l &>/dev/null
|
|
if (( ? == 2 )); then
|
|
# Start agent and store agent connection info
|
|
(umask 066; ssh-agent >! ${ssh_env})
|
|
source ${ssh_env} >/dev/null
|
|
fi
|
|
fi
|
|
|
|
# Load identities
|
|
ssh-add -l &>/dev/null
|
|
if (( ? == 1 )); then
|
|
if (( ${#zssh_ids} > 0 )); then
|
|
ssh-add "${HOME}/.ssh/${^zssh_ids[@]}" 2> /dev/null
|
|
else
|
|
ssh-add 2> /dev/null
|
|
fi
|
|
fi
|