1
0
mirror of https://github.com/dcarrillo/prezto.git synced 2024-11-20 08:31:13 +00:00
prezto/modules/ssh/init.zsh

51 lines
1.4 KiB
Bash
Raw Normal View History

#
# Provides for an easier use of SSH by setting up ssh-agent.
#
# Authors:
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
# Return if requirements are not found.
2017-05-05 16:58:19 +00:00
if (( ! $+commands[ssh-agent] )); then
return 1
fi
# Set the path to the SSH directory.
_ssh_dir="$HOME/.ssh"
# Set the path to the environment file if not set by another module.
_ssh_agent_env="${_ssh_agent_env:-${TMPDIR:-/tmp}/ssh-agent.env.$UID}"
# Set the path to the persistent authentication socket.
_ssh_agent_sock="${TMPDIR:-/tmp}/ssh-agent.sock.$UID"
# Start ssh-agent if not started.
if [[ ! -S "$SSH_AUTH_SOCK" ]]; then
# Export environment variables.
source "$_ssh_agent_env" 2> /dev/null
# Start ssh-agent if not started.
if ! ps -U "$LOGNAME" -o pid,ucomm | grep -q -- "${SSH_AGENT_PID:--1} ssh-agent"; then
eval "$(ssh-agent | sed '/^echo /d' | tee "$_ssh_agent_env")"
fi
fi
# Create a persistent SSH authentication socket.
if [[ -S "$SSH_AUTH_SOCK" && "$SSH_AUTH_SOCK" != "$_ssh_agent_sock" ]]; then
ln -sf "$SSH_AUTH_SOCK" "$_ssh_agent_sock"
export SSH_AUTH_SOCK="$_ssh_agent_sock"
fi
# Load identities.
if ssh-add -l 2>&1 | grep -q 'The agent has no identities'; then
zstyle -a ':prezto:module:ssh:load' identities '_ssh_identities'
if (( ${#_ssh_identities} > 0 )); then
2017-06-19 18:54:54 +00:00
ssh-add "$_ssh_dir/${^_ssh_identities[@]}" < /dev/null 2> /dev/null
else
2017-06-19 18:54:54 +00:00
ssh-add < /dev/null 2> /dev/null
fi
fi
# Clean up.
unset _ssh_{dir,identities} _ssh_agent_{env,sock}