Added improved ssh-agent support.
This commit is contained in:
parent
9b522912b3
commit
ace5e0e8e3
3 changed files with 174 additions and 28 deletions
|
|
@ -35,43 +35,62 @@ unset _PLATFORM
|
|||
[ -d "$HOME/.local/bin" ] && export PATH="$HOME/.local/bin:$PATH"
|
||||
[ -d "$HOME/bin" ] && export PATH="$HOME/bin:$PATH"
|
||||
|
||||
# SSH agent.
|
||||
# Make ssh-agent work better.
|
||||
hash ssh-add ssh-agent >/dev/null 2>&1 && {
|
||||
if [ ! -z "$SSH_AUTH_SOCK" ]; then
|
||||
ssh-add -l >/dev/null 2>&1
|
||||
if (( $? < 2 )); then
|
||||
if [[ -z "$SSH_AUTH_SOCK" ]]; then
|
||||
# Try to find an existing agent socket to connect to.
|
||||
SSH_AUTH_SOCK="$(_find_agent_sock)"
|
||||
if ((${PIPESTATUS[0]} == 0)); then
|
||||
# Found a socket.
|
||||
export SSH_AUTH_SOCK
|
||||
echo "Connected to ssh-agent."
|
||||
echo
|
||||
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
|
||||
else
|
||||
echo -e '$(tput setaf 1 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)'
|
||||
echo
|
||||
fi
|
||||
else
|
||||
. ~/.ssh/agent 2>/dev/null
|
||||
ssh-add -l >/dev/null 2>&1
|
||||
if (( $? < 2 )); then
|
||||
echo "Connected to ssh-agent."
|
||||
echo
|
||||
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
|
||||
else
|
||||
if ssh-agent -s | sed -e '/^echo/ d' >~/.ssh/agent; then
|
||||
. ~/.ssh/agent
|
||||
ssh-add -l >/dev/null 2>&1
|
||||
if (( $? < 2 )); then
|
||||
echo "Started new ssh-agent."
|
||||
echo
|
||||
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
|
||||
# No viable socket - try to start an agent.
|
||||
eval "$(ssh-agent -s | grep -v 'echo')"
|
||||
ssh-add -l >/dev/null 2>&1
|
||||
if (( $? < 2 )); then
|
||||
# Agent started - add new socket to the agent sockets file.
|
||||
exec 9>~/.ssh/agents.lock
|
||||
if flock -E 10 -w 0.5 9; then
|
||||
if _push_agent_sock; then
|
||||
echo "Started new ssh-agent."
|
||||
else
|
||||
echo "$(tput setaf 1 || tput AF 1)Started new ssh-agent, but failed to register socket!$(tput op)"
|
||||
fi
|
||||
else
|
||||
echo "$(tput setaf 1 || tput AF 1)Problem connecting to local ssh-agent!$(tput op)"
|
||||
echo
|
||||
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
|
||||
fi
|
||||
exec 9>&-
|
||||
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
|
||||
else
|
||||
echo "$(tput setaf 1 || tput AF 1)Failed to start local ssh-agent!$(tput op)"
|
||||
echo
|
||||
echo "$(tput setaf 1 || tput AF 1)Failed to start new ssh-agent!$(tput op)"
|
||||
unset SSH_AGENT_PID SSH_AUTH_SOCK
|
||||
fi
|
||||
fi
|
||||
else
|
||||
# Got a forwarded ssh-agent connection.
|
||||
export SSH_AUTH_SOCK
|
||||
ssh-add -l >/dev/null 2>&1
|
||||
if (( $? < 2 )); then
|
||||
# Agent is connected - add new socket to the agent sockets file.
|
||||
exec 9>~/.ssh/agents.lock
|
||||
if flock -E 10 -w 0.5 9; then
|
||||
if _push_agent_sock; then
|
||||
echo "Connected to forwarded ssh-agent."
|
||||
else
|
||||
echo -e "$(tput setaf 1 || tput AF 1)Failed to register forwarded ssh-agent socket!$(tput op)"
|
||||
fi
|
||||
else
|
||||
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
|
||||
fi
|
||||
exec 9>&-
|
||||
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
|
||||
else
|
||||
echo -e "$(tput setaf 1 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)"
|
||||
fi
|
||||
fi
|
||||
echo
|
||||
}
|
||||
|
||||
# Screen.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue