From 29b1645c710bcb373f0e3baee35615a3939210d4 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Tue, 2 Jul 2019 21:36:05 +0100 Subject: [PATCH] New ssh-agent login logic. --- .bash_profile | 153 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 116 insertions(+), 37 deletions(-) diff --git a/.bash_profile b/.bash_profile index f9227cd..bfcd258 100644 --- a/.bash_profile +++ b/.bash_profile @@ -36,56 +36,135 @@ fi # Make ssh-agent work better. hash ssh-add ssh-agent >/dev/null 2>&1 && { - 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." - 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')" - ssh-add -l >/dev/null 2>&1 - if (( $? < 2 )); then - # Agent started - add new socket to the agent sockets file. - _lock_agents_file && { - 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 - } - _unlock_agents_file - 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 new ssh-agent!$(tput op)" - unset SSH_AGENT_PID SSH_AUTH_SOCK - fi - fi - else - # Got a forwarded ssh-agent connection. + if [[ -e "$SSH_AUTH_SOCK" ]]; then + # Got a possible 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. + _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 forwarded ssh-agent." + echo "Connected to ssh-agent." else - echo -e "$(tput setaf 1 || tput AF 1)Failed to register forwarded ssh-agent socket!$(tput op)" + echo -e "$(tput setaf 3)Connected to ssh-agent, but failed to register socket.$(tput op)" + # FIXME: Set marker here that we failed. 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 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)" + # 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)" + # FIXME: Set marker here that we failed. + 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)" + # FIXME: Set marker here that we failed. + 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)" + # FIXME: Set marker here that we failed. + 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 _PLATFORM +unset _OUTPUT _PLATFORM + +#hash ssh-add ssh-agent >/dev/null 2>&1 && { +# 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." +# 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')" +# ssh-add -l >/dev/null 2>&1 +# if (( $? < 2 )); then +# # Agent started - add new socket to the agent sockets file. +# _lock_agents_file && { +# 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 +# } +# _unlock_agents_file +# 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 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. +# _lock_agents_file && { +# 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 +# } +# _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 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)" +# fi +# fi +# echo +#} +#unset _PLATFORM # Screen. hash screen >/dev/null 2>&1 && {