Remove (partially) broken ssh-agent handling code. Don't use _ for temp variables.

This commit is contained in:
Darren 'Tadgy' Austin 2021-03-27 05:22:21 +00:00
commit 5ba3e060b2
2 changed files with 24 additions and 257 deletions

View file

@ -13,108 +13,29 @@ export VISUAL="$EDITOR"
[[ -x /usr/bin/lesspipe ]] && eval "$(SHELL=/bin/sh lesspipe)" [[ -x /usr/bin/lesspipe ]] && eval "$(SHELL=/bin/sh lesspipe)"
# Platform specific set up. # Platform specific set up.
_PLATFORM="$(uname -s)" PLATFORM="$(uname -s)"
if [[ "$_PLATFORM" = "Linux" ]]; then if [[ "$PLATFORM" = "Linux" ]]; then
export I_WANT_A_BROKEN_PS=1 export I_WANT_A_BROKEN_PS=1
export LYNX_CFG="$HOME/.lynx.cfg" export LYNX_CFG="$HOME/.lynx.cfg"
export LYNX_LSS="$HOME/.lynx.lss" export LYNX_LSS="$HOME/.lynx.lss"
export MANPAGER="less" export MANPAGER="less"
export MANPATH="$HOME/.local/share/man:$MANPATH" export MANPATH="$HOME/.local/share/man:$MANPATH"
hash dircolors >/dev/null 2>&1 && eval "$(dircolors -b ~/.dir_colors)" hash dircolors >/dev/null 2>&1 && eval "$(dircolors -b ~/.dir_colors)"
elif [[ "$_PLATFORM" = "Darwin" ]]; then elif [[ "$PLATFORM" = "Darwin" ]]; then
export LSCOLORS="ExGxdxdxCxDxDxbcacbeae" export LSCOLORS="ExGxdxdxCxDxDxbcacbeae"
export MANPAGER="less -Mis -PM'Page %dt$'" export MANPAGER="less -Mis -PM'Page %dt$'"
export MANPATH="/opt/local/share/man:$MANPATH" export MANPATH="/opt/local/share/man:$MANPATH"
export PATH="/opt/local/sbin:/opt/local/bin:$PATH" export PATH="/opt/local/sbin:/opt/local/bin:$PATH"
else else
echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2 echo "${BASH_SOURCE##*/}: unsupported platform: $PLATFORM" >&2
fi fi
unset PLATFORM
# Add bin directories to PATH. # Add bin directories to PATH.
[[ -d "$HOME/files/bin" ]] && export PATH="$HOME/files/bin:$PATH" [[ -d "$HOME/files/bin" ]] && export PATH="$HOME/files/bin:$PATH"
[[ -d "$HOME/.local/bin" ]] && export PATH="$HOME/.local/bin:$PATH" [[ -d "$HOME/.local/bin" ]] && export PATH="$HOME/.local/bin:$PATH"
[[ -d "$HOME/bin" ]] && export PATH="$HOME/bin:$PATH" [[ -d "$HOME/bin" ]] && export PATH="$HOME/bin:$PATH"
# Make ssh-agent work better.
hash ssh-add ssh-agent >/dev/null 2>&1 && {
if [[ -e "$SSH_AUTH_SOCK" ]]; then
# Got a possible ssh-agent connection.
export SSH_AUTH_SOCK
_OUTPUT="$(ssh-add -l 2>&1 >/dev/null)"
if (( ${PIPESTATUS[0]} < 2 )) && [[ -z "$_OUTPUT" ]]; then
# Agent is connected.
_lock_agents_file && {
if _push_agent_sock; then
echo "Connected to ssh-agent."
else
echo -e "$(tput setaf 3)Connected to ssh-agent, but failed to register socket.$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
# Agent connection failed.
SSH_AUTH_SOCK="$(_find_agent_sock)"
if ((${PIPESTATUS[0]} == 0)); then
# Found a new socket.
export SSH_AUTH_SOCK
_lock_agents_file && {
if _push_agent_sock; then
echo "Connected to alternate ssh-agent - you may need to re-add keys."
else
echo "$(tput setaf 3)Connected to, but failed to register, alternate ssh-agent - you may need to re-add keys.$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
echo -e "$(tput setaf 1)Lost connection to ssh-agent - no alternate available!$(tput op)"
fi
fi
else
# No ssh-agent connection.
SSH_AUTH_SOCK="$(_find_agent_sock)"
if ((${PIPESTATUS[0]} == 0)); then
# Found a socket.
export SSH_AUTH_SOCK
_lock_agents_file && {
if _push_agent_sock; then
echo "Connected to ssh-agent."
else
echo "$(tput setaf 3)Connected to, but failed to register, ssh-agent.$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
# No viable socket - try to start an agent.
eval "$(ssh-agent -s | grep -v 'echo')"
_OUTPUT="$(ssh-add -l 2>&1 >/dev/null)"
if (( ${PIPESTATUS[0]} < 2 )) && [[ -z "$_OUTPUT" ]]; then
# Agent started - register socket in the agents file.
_lock_agents_file && {
if _push_agent_sock; then
echo "Started new ssh-agent."
else
echo "$(tput setaf 3)Started, but failed to register, new ssh-agent$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else
echo "$(tput setaf 1)Failed to start new ssh-agent!$(tput op)"
unset SSH_AGENT_PID SSH_AUTH_SOCK
fi
fi
fi
echo
}
unset _OUTPUT _PLATFORM
# Screen. # Screen.
hash screen >/dev/null 2>&1 && { hash screen >/dev/null 2>&1 && {
# Reattach existing screens. # Reattach existing screens.
@ -127,15 +48,15 @@ hash screen >/dev/null 2>&1 && {
echo "Screen $STY, window $WINDOW." echo "Screen $STY, window $WINDOW."
echo echo
else else
_SCREENS="$(screen -ls | grep '[[:alpha:]]' | egrep -v '^([[:digit:]]+|No) Socket(s)?')" SCREENS="$(screen -ls | grep '[[:alpha:]]' | egrep -v '^([[:digit:]]+|No) Socket(s)?')"
if [[ -n "$_SCREENS" ]]; then if [[ -n "$SCREENS" ]]; then
echo "$_SCREENS" echo "$SCREENS"
echo echo
else else
echo "No screens." echo "No screens."
echo echo
fi fi
unset _SCREENS unset SCREENS
fi fi
fi fi
} }

184
.bashrc
View file

@ -1,158 +1,4 @@
# Functions. # Functions.
_agent_prompt_command() {
hash ssh-add >/dev/null 2>&1 && {
local OUTPUT SOCK _PLATFORM="$(uname -s)"
declare -g _SSH_PROMPT_ERROR_ISSUED
# Check the ssh agent socket is still alive.
# Need to work around an ssh-add bug here: ssh-add doesn't always return 2 on failure
# to contact the agent, so also check if any errors were produced.
OUTPUT="$(ssh-add -l 2>&1 >/dev/null)"
if ((${PIPESTATUS[0]} >= 2)) || [[ ! -z "$OUTPUT" ]]; then
# Auth socket has become unusable, search for a new one.
SOCK="$(_find_agent_sock)"
if ((${PIPESTATUS[0]} == 0)); then
export SSH_AUTH_SOCK="$SOCK"
_lock_agents_file && {
if _push_agent_sock; then
echo "Connected to alternate ssh-agent - you may need to re-add keys."
_SSH_AGENT_REG_FAILED=0
else
echo "$(tput setaf 3)Connected to, but failed to register, alternate ssh-agent - you may need to re-add keys.$(tput op)"
_SSH_AGENT_REG_FAILED=1
fi
}
_unlock_agents_file
_SSH_PROMPT_ERROR_ISSUED=0
else
# Failed to find a new socket.
if [[ -z "$(egrep -v '^$' ~/.ssh/agents)" ]]; then
((${_SSH_PROMPT_ERROR_ISSUED:-0} == 0)) && {
echo "$(tput setaf 1)Lost connection to ssh-agent - no alternate available!$(tput op)"
unset SSH_AUTH_SOCK
_SSH_PROMPT_ERROR_ISSUED=1
}
else
((${_SSH_PROMPT_ERROR_ISSUED:-0} == 0)) && {
echo "$(tput setaf 1)Lost connection to ssh-agent - failed to connect to new agent!$(tput op)"
unset SSH_AUTH_SOCK
_SSH_PROMPT_ERROR_ISSUED=1
}
fi
fi
else
_SSH_PROMPT_ERROR_ISSUED=0
fi
}
}
_clean_agent_socks() {
local I OUTPUT SSH_AUTH_SOCK
# Go through the array of sockets and validate each one.
for ((I = 0; I < ${#SOCKS[@]}; I++)); do
OUTPUT="$(SSH_AUTH_SOCK="${SOCKS[$I]}" ssh-add -l 2>&1 >/dev/null)"
((${PIPESTATUS[0]} >= 2)) || [[ ! -z "$OUTPUT" ]] && {
unset SOCKS[$I]
}
done
}
_find_agent_sock() {
local I IFS=$'\n' OUTPUT REPLY SOCKS=() SSH_AUTH_SOCK="$SSH_AUTH_SOCK"
# Load the array of sockets, minus the one we know is unusable.
if ((${BASH_VERSINFO[0]} >= 4)); then
mapfile -t SOCKS < <(egrep -v "^$SSH_AUTH_SOCK\$" ~/.ssh/agents 2>/dev/null)
else
while read -r; do
SOCKS+=("$REPLY")
done < <(egrep -v "^$SSH_AUTH_SOCK\$" ~/.ssh/agents 2>/dev/null)
fi
# Search backwards through the list to find an active socket.
for ((I = (${#SOCKS[@]} - 1); I >= 0; I--)); do
OUTPUT="$(SSH_AUTH_SOCK="${SOCKS[$I]}" ssh-add -l 2>&1 >/dev/null)"
{ ((${PIPESTATUS[0]} <= 1)) && [[ -z "$OUTPUT" ]]; } && [[ ! -z "${SOCKS[$I]}" ]] && {
printf "%s" "${SOCKS[$I]}"
return 0
}
done
return 1
}
_lock_agents_file() {
local I
# Lock the ~/.ssh/agents file.
if [[ "$_PLATFORM" == "Linux" ]]; then
# Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
return 0
else
echo "$(tput setaf 1)Failed to obtain lockfile!$(tput op)"
fi
elif [[ "$_PLATFORM" == "Darwin" ]]; then
# Do locking the sucky way on OSX.
for ((I=0; I < 5; I++)); do
if shlock -f "/tmp/agents.lock.$$" -p "$$"; then
return 0
else
sleep 0.1
fi
done
[[ "${_GOT_LOCK:-0}" == "0" ]] && echo "$(tput setaf 1)Failed to obtain lockfile!$(tput op)"
else
echo "$(tput setaf 1)File locking unsupported - skipping update of agents file!$(tput op)"
fi
return 1
}
_pop_agent_sock() {
local I IFS=$'\n' REPLY SOCKS=()
# Read the current list of auth sockets.
if ((${BASH_VERSINFO[0]} >= 4)); then
mapfile -t SOCKS <~/.ssh/agents
else
while read -r; do
SOCKS+=("$REPLY")
done <~/.ssh/agents
fi
# Remove the last instance of the socket in $SSH_AUTH_SOCK.
for ((I = (${#SOCKS[@]} - 1); I >= 0; I--)); do
[[ "${SOCKS[$I]}" == "$SSH_AUTH_SOCK" ]] && {
unset SOCKS[$I]
break
}
done
# Clean up any dead sockets - this modifies the SOCKS array.
_clean_agent_socks
# Write the new list back to disk.
printf "%s\n" "${SOCKS[@]}" >~/.ssh/agents
return $?
}
_push_agent_sock() {
local IFS=$'\n' REPLY SOCKS=()
# Read the current list of auth sockets.
if ((${BASH_VERSINFO[0]} >= 4)); then
mapfile -t SOCKS <~/.ssh/agents
else
while read -r; do
SOCKS+=("$REPLY")
done <~/.ssh/agents
fi
# Clean up any dead sockets - this modifies the SOCKS array.
_clean_agent_socks
# Write the new list back to disk.
printf "%s\n" "${SOCKS[@]}" "$SSH_AUTH_SOCK" >~/.ssh/agents
return $?
}
_unlock_agents_file() {
# Unlock the ~/.ssh/agents file.
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
}
screen_attach() { screen_attach() {
local TTY SCREENS local TTY SCREENS
# Only continue if it's not a remote connection, and not inside screen already. # Only continue if it's not a remote connection, and not inside screen already.
@ -194,9 +40,9 @@ history -r
# Determine the colour of the username in the prompt. # Determine the colour of the username in the prompt.
if (( $(id -u) == 0 )); then if (( $(id -u) == 0 )); then
_COLOUR=1 # Red COLOUR=1 # Red
else else
_COLOUR=2 # Green COLOUR=2 # Green
fi fi
# Version specific set up. # Version specific set up.
@ -209,23 +55,23 @@ if (( ${BASH_VERSINFO[0]} >= 4 )); then
# No colour: # No colour:
# PS1="[\u@\h] \w ->" # PS1="[\u@\h] \w ->"
# Coloured username: # Coloured username:
# PS1="[\[$(tput bold)$(tput setaf $_COLOUR)\]\u\[$(tput sgr0)\]@\h] \w ->" # PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\h] \w ->"
# Coloured username + host: # Coloured username + host:
# PS1="[\[$(tput bold)$(tput setaf $_COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \w ->" # PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \w ->"
# Coloured username + host + directory: # Coloured username + host + directory:
PS1="[\[$(tput bold)$(tput setaf $_COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\w\[$(tput sgr0)\] ->" PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\w\[$(tput sgr0)\] ->"
else else
# Set the prompts. # Set the prompts.
# No colour: # No colour:
# PS1="[\u@\h] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->" # PS1="[\u@\h] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->"
# Coloured username: # Coloured username:
# PS1="[\[$(tput bold)$(tput setaf $_COLOUR)\]\u\[$(tput sgr0)\]@\h] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->" # PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\h] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->"
# Coloured username + host: # Coloured username + host:
# PS1="[\[$(tput bold)$(tput setaf $_COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->" # PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->"
# Coloured username + host + directory: # Coloured username + host + directory:
PS1="[\[$(tput bold)$(tput setaf $_COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}')\[$(tput sgr0)\] ->" PS1="[\[$(tput bold)$(tput setaf $COLOUR)\]\u\[$(tput sgr0)\]@\[$(tput bold)$(tput setaf 3)\]\h\[$(tput sgr0)\]] \[$(tput bold)$(tput setaf 4)\]\$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}')\[$(tput sgr0)\] ->"
fi fi
unset _COLOUR unset COLOUR
# Set the debugger prompt. # Set the debugger prompt.
# PS4="+(\\\$? = \$?) \${BASH_SOURCE##*/}\${FUNCNAME:+(\$FUNCNAME)}:\$LINENO: " # PS4="+(\\\$? = \$?) \${BASH_SOURCE##*/}\${FUNCNAME:+(\$FUNCNAME)}:\$LINENO: "
@ -233,11 +79,11 @@ unset _COLOUR
export PS4="+(\[\e[33m\]\$?\[$(tput sgr0)\]) \[$(tput bold)$(tput setaf 4)\]\${BASH_SOURCE##*/}\[$(tput sgr0)\]\${FUNCNAME:+(\[$(tput bold)$(tput setaf 2)\]\$FUNCNAME\[$(tput sgr0)\])}:\[$(tput bold)$(tput setaf 1)\]\$LINENO\[$(tput sgr0)\]: " export PS4="+(\[\e[33m\]\$?\[$(tput sgr0)\]) \[$(tput bold)$(tput setaf 4)\]\${BASH_SOURCE##*/}\[$(tput sgr0)\]\${FUNCNAME:+(\[$(tput bold)$(tput setaf 2)\]\$FUNCNAME\[$(tput sgr0)\])}:\[$(tput bold)$(tput setaf 1)\]\$LINENO\[$(tput sgr0)\]: "
# The commands to execute before the prompt is displayed. # The commands to execute before the prompt is displayed.
PROMPT_COMMAND="_agent_prompt_command" # PROMPT_COMMAND=""
# Platform specific set up. # Platform specific set up.
_PLATFORM="$(uname -s)" PLATFORM="$(uname -s)"
if [[ "$_PLATFORM" = "Linux" ]]; then if [[ "$PLATFORM" = "Linux" ]]; then
# Linux specific aliases. # Linux specific aliases.
hash ftpwho >/dev/null 2>&1 && alias ftpwho='ftpwho -v' hash ftpwho >/dev/null 2>&1 && alias ftpwho='ftpwho -v'
hash iftop >/dev/null 2>&1 && alias iftop='TERM=vt100 iftop' hash iftop >/dev/null 2>&1 && alias iftop='TERM=vt100 iftop'
@ -253,15 +99,15 @@ if [[ "$_PLATFORM" = "Linux" ]]; then
psgrep() { psgrep() {
ps -auwwx | egrep "(RSS|$1)" | fgrep -v "(RSS|" ps -auwwx | egrep "(RSS|$1)" | fgrep -v "(RSS|"
} }
elif [[ "$_PLATFORM" = "Darwin" ]]; then elif [[ "$PLATFORM" = "Darwin" ]]; then
# Darwin specific aliases. # Darwin specific aliases.
hash df >/dev/null 2>&1 && alias df='df -P' hash df >/dev/null 2>&1 && alias df='df -P'
hash ls >/dev/null 2>&1 && alias ls='ls -bFG' hash ls >/dev/null 2>&1 && alias ls='ls -bFG'
hash top >/dev/null 2>&1 && alias top='top -o cpu -S' hash top >/dev/null 2>&1 && alias top='top -o cpu -S'
else else
echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2 echo "${BASH_SOURCE##*/}: unsupported platform: $PLATFORM" >&2
fi fi
unset _PLATFORM unset PLATFORM
# Common aliases. # Common aliases.
hash bc >/dev/null 2>&1 && alias bc='bc -lq' hash bc >/dev/null 2>&1 && alias bc='bc -lq'