[ssh] Update init.zsh
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 mode and in the user home directory. About not using `$` inside `(( ))`, this is what the the section ARITHMETIC EVALUATION in zshmisc(1) says: > Named parameters and subscripted arrays can be referenced by name > within an arithmetic expression without using the parameter expansion > syntax. And according to http://www.bash2zsh.com/zsh_refcard/refcard.pdf: > `var` (does not require `$` in front unless some substitution e.g. > `${#var}` is needed, `$` is error if `var` is to be modified) Closes #292
This commit is contained in:
parent
df672688de
commit
edd4e82d8a
1 changed files with 14 additions and 26 deletions
|
@ -7,40 +7,28 @@ if (( ! ${+commands[ssh-agent]} )); then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# use a sane temp dir; creating 1k ssh-* files in /tmp is crazy
|
ssh-add -l &>/dev/null
|
||||||
if [[ ${TMPDIR} ]]; then
|
if (( ? == 2 )); then
|
||||||
local ssh_env=${TMPDIR}/ssh-agent.env
|
# Unable to contact the authentication agent
|
||||||
local ssh_sock=${TMPDIR}/ssh-agent.sock
|
|
||||||
else
|
|
||||||
# create a sane tmp dir at /tmp/username
|
|
||||||
mkdir -p /tmp/${USER}
|
|
||||||
local ssh_env=/tmp/${USER}/ssh-agent.env
|
|
||||||
local ssh_sock=/tmp/${USER}/ssh-agent.sock
|
|
||||||
fi
|
|
||||||
|
|
||||||
# start ssh-agent if not already running
|
# Load stored agent connection info
|
||||||
if [[ ! -S ${SSH_AUTH_SOCK} ]]; then
|
local ssh_env="${HOME}/.ssh-agent"
|
||||||
# read environment if possible
|
[[ -r ${ssh_env} ]] && source ${ssh_env} >/dev/null
|
||||||
source ${ssh_env} 2> /dev/null
|
|
||||||
|
|
||||||
if ! ps -U ${LOGNAME} -o pid,ucomm | grep -q -- "${SSH_AGENT_PID:--1} ssh-agent"; then
|
ssh-add -l &>/dev/null
|
||||||
eval "$(ssh-agent | sed '/^echo /d' | tee ${ssh_env})"
|
if (( ? == 2 )); then
|
||||||
|
# Start agent and store agent connection info
|
||||||
|
(umask 066; ssh-agent >! ${ssh_env})
|
||||||
|
source ${ssh_env} >/dev/null
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# create socket
|
# Load identities
|
||||||
if [[ -S ${SSH_AUTH_SOCKET} && ${SSH_AUTH_SOCKET} != ${ssh_sock} ]]; then
|
ssh-add -l &>/dev/null
|
||||||
ln -sf ${SSH_AUTH_SOCKET} ${ssh_sock}
|
if (( ? == 1 )); then
|
||||||
export SSH_AUTH_SOCK=${ssh_sock}
|
|
||||||
fi
|
|
||||||
|
|
||||||
# load ids
|
|
||||||
if ssh-add -l 2>&1 | grep -q 'no identities'; then
|
|
||||||
if (( ${#zssh_ids} > 0 )); then
|
if (( ${#zssh_ids} > 0 )); then
|
||||||
ssh-add "${HOME}/.ssh/${^zssh_ids[@]}" 2> /dev/null
|
ssh-add "${HOME}/.ssh/${^zssh_ids[@]}" 2> /dev/null
|
||||||
else
|
else
|
||||||
ssh-add 2> /dev/null
|
ssh-add 2> /dev/null
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
unset ssh_{sock,env}
|
|
||||||
|
|
Loading…
Reference in a new issue