Remove (partially) broken ssh-agent handling code. Don't use _ for temp variables.
This commit is contained in:
parent
feadf172cb
commit
5ba3e060b2
2 changed files with 24 additions and 257 deletions
184
.bashrc
184
.bashrc
|
@ -1,158 +1,4 @@
|
|||
# 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() {
|
||||
local TTY SCREENS
|
||||
# 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.
|
||||
if (( $(id -u) == 0 )); then
|
||||
_COLOUR=1 # Red
|
||||
COLOUR=1 # Red
|
||||
else
|
||||
_COLOUR=2 # Green
|
||||
COLOUR=2 # Green
|
||||
fi
|
||||
|
||||
# Version specific set up.
|
||||
|
@ -209,23 +55,23 @@ if (( ${BASH_VERSINFO[0]} >= 4 )); then
|
|||
# No colour:
|
||||
# PS1="[\u@\h] \w ->"
|
||||
# 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:
|
||||
# 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:
|
||||
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
|
||||
# Set the prompts.
|
||||
# No colour:
|
||||
# PS1="[\u@\h] \$(echo \"\${PWD/#\$HOME/~}\" | awk -F/ '{if (NF>3) {printf \".../\" \$(NF-1) \"/\" \$NF} else {printf \$0}}') ->"
|
||||
# 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:
|
||||
# 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:
|
||||
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
|
||||
unset _COLOUR
|
||||
unset COLOUR
|
||||
|
||||
# Set the debugger prompt.
|
||||
# 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)\]: "
|
||||
|
||||
# The commands to execute before the prompt is displayed.
|
||||
PROMPT_COMMAND="_agent_prompt_command"
|
||||
# PROMPT_COMMAND=""
|
||||
|
||||
# Platform specific set up.
|
||||
_PLATFORM="$(uname -s)"
|
||||
if [[ "$_PLATFORM" = "Linux" ]]; then
|
||||
PLATFORM="$(uname -s)"
|
||||
if [[ "$PLATFORM" = "Linux" ]]; then
|
||||
# Linux specific aliases.
|
||||
hash ftpwho >/dev/null 2>&1 && alias ftpwho='ftpwho -v'
|
||||
hash iftop >/dev/null 2>&1 && alias iftop='TERM=vt100 iftop'
|
||||
|
@ -253,15 +99,15 @@ if [[ "$_PLATFORM" = "Linux" ]]; then
|
|||
psgrep() {
|
||||
ps -auwwx | egrep "(RSS|$1)" | fgrep -v "(RSS|"
|
||||
}
|
||||
elif [[ "$_PLATFORM" = "Darwin" ]]; then
|
||||
elif [[ "$PLATFORM" = "Darwin" ]]; then
|
||||
# Darwin specific aliases.
|
||||
hash df >/dev/null 2>&1 && alias df='df -P'
|
||||
hash ls >/dev/null 2>&1 && alias ls='ls -bFG'
|
||||
hash top >/dev/null 2>&1 && alias top='top -o cpu -S'
|
||||
else
|
||||
echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2
|
||||
echo "${BASH_SOURCE##*/}: unsupported platform: $PLATFORM" >&2
|
||||
fi
|
||||
unset _PLATFORM
|
||||
unset PLATFORM
|
||||
|
||||
# Common aliases.
|
||||
hash bc >/dev/null 2>&1 && alias bc='bc -lq'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue