diff options
author | Mike Crute <mike@crute.us> | 2022-09-24 09:35:36 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2022-09-24 09:35:36 -0700 |
commit | b1262b8f4e8199d067390617b3362dd44c667b6e (patch) | |
tree | ba72803ff51ac8e74c0bdef9b0174846f7a710e6 | |
parent | c2eb97f3c1aa81f1065a224c80d495f9623dd546 (diff) | |
download | dotfiles-b1262b8f4e8199d067390617b3362dd44c667b6e.tar.bz2 dotfiles-b1262b8f4e8199d067390617b3362dd44c667b6e.tar.xz dotfiles-b1262b8f4e8199d067390617b3362dd44c667b6e.zip |
Prefer running ssh agent
-rw-r--r-- | .bashrc | 55 |
1 files changed, 32 insertions, 23 deletions
@@ -633,31 +633,40 @@ function get_ssh_agent | |||
633 | esac | 633 | esac |
634 | done | 634 | done |
635 | 635 | ||
636 | for path in "${TMPDIR:-/tmp}"/ssh-*/agent.*; do | 636 | # Prefer to use the running agent if there's one already running. |
637 | basepath=$(basename $path) | 637 | # Otherwise try to figure out where a suitable agent is and connect. |
638 | sockpath=$(dirname $path) | 638 | # to it. |
639 | 639 | if ! _ssh_agent_is_running; then | |
640 | export SSH_AUTH_SOCK=${path} | 640 | [[ -n "$debug" ]] && echo "No agent running, commencing search..." |
641 | export SSH_AGENT_PID=${basepath##agent.} | 641 | |
642 | 642 | for path in "${TMPDIR:-/tmp}"/ssh-*/agent.*; do | |
643 | # When no files are present matching the pattern the pattern itself is | 643 | basepath=$(basename $path) |
644 | # passed through. shopt -s nullglob would fix this but too much | 644 | sockpath=$(dirname $path) |
645 | # fiddling to make it work without polluting the parent shell state. | 645 | |
646 | if [[ "$SSH_AGENT_PID" == "*" ]]; then | 646 | export SSH_AUTH_SOCK=${path} |
647 | [[ -n "$debug" ]] && echo "No agents found" | 647 | export SSH_AGENT_PID=${basepath##agent.} |
648 | break | 648 | |
649 | fi | 649 | # When no files are present matching the pattern the pattern itself is |
650 | # passed through. shopt -s nullglob would fix this but too much | ||
651 | # fiddling to make it work without polluting the parent shell state. | ||
652 | if [[ "$SSH_AGENT_PID" == "*" ]]; then | ||
653 | [[ -n "$debug" ]] && echo "No agents found" | ||
654 | break | ||
655 | fi | ||
650 | 656 | ||
651 | [[ -n "$debug" ]] && echo -n "Trying ${SSH_AUTH_SOCK} for ${SSH_AGENT_PID}... " | 657 | [[ -n "$debug" ]] && echo -n "Trying ${SSH_AUTH_SOCK} for ${SSH_AGENT_PID}... " |
652 | 658 | ||
653 | if _ssh_agent_is_running; then | 659 | if _ssh_agent_is_running; then |
654 | [[ -n "$debug" ]] && echo "Success" | 660 | [[ -n "$debug" ]] && echo "Success" |
655 | break | 661 | break |
656 | else | 662 | else |
657 | [[ -n "$debug" ]] && echo "Failure... removing ${sockpath}" | 663 | [[ -n "$debug" ]] && echo "Failure... removing ${sockpath}" |
658 | rm -rf "$sockpath" | 664 | rm -rf "$sockpath" |
659 | fi | 665 | fi |
660 | done | 666 | done |
667 | else | ||
668 | [[ -n "$debug" ]] && echo "Using already running agent..." | ||
669 | fi | ||
661 | 670 | ||
662 | # If the agent still isn't connected after sourcing our local agent | 671 | # If the agent still isn't connected after sourcing our local agent |
663 | # state file then it probably isn't running at all. Start a new | 672 | # state file then it probably isn't running at all. Start a new |