diff options
author | Mike Crute <mike@crute.us> | 2021-04-04 17:38:58 +0000 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2021-04-04 17:40:41 +0000 |
commit | dfe8abbbe0374faf01477fb806f8ff31639d25f2 (patch) | |
tree | 84b47e55c3e46d7628a39403259fb8b9372b4111 /.bashrc | |
parent | 883fcbf3e27ec4eb2738fb8447e5c273e880a5cb (diff) | |
download | dotfiles-dfe8abbbe0374faf01477fb806f8ff31639d25f2.tar.bz2 dotfiles-dfe8abbbe0374faf01477fb806f8ff31639d25f2.tar.xz dotfiles-dfe8abbbe0374faf01477fb806f8ff31639d25f2.zip |
Make SSH agent finder more accurate
Diffstat (limited to '.bashrc')
-rw-r--r-- | .bashrc | 53 |
1 files changed, 35 insertions, 18 deletions
@@ -594,10 +594,14 @@ function get_ssh_agent | |||
594 | { | 594 | { |
595 | local start_agent="" | 595 | local start_agent="" |
596 | local load_keys="" | 596 | local load_keys="" |
597 | local debug="" | ||
597 | 598 | ||
598 | local OPTIND opt | 599 | local OPTIND opt |
599 | while getopts "sklh" opt; do | 600 | while getopts "dsklh" opt; do |
600 | case $opt in | 601 | case $opt in |
602 | d) # debug | ||
603 | debug="1" | ||
604 | ;; | ||
601 | s) # start-agent | 605 | s) # start-agent |
602 | start_agent="1" | 606 | start_agent="1" |
603 | ;; | 607 | ;; |
@@ -609,7 +613,8 @@ function get_ssh_agent | |||
609 | load_keys="1" | 613 | load_keys="1" |
610 | ;; | 614 | ;; |
611 | h) # help | 615 | h) # help |
612 | echo "Usage: get_ssh_agent [sklh]" | 616 | echo "Usage: get_ssh_agent [dsklh]" |
617 | echo " -d : Emit debug messages" | ||
613 | echo " -s : Start an agent if one is not running" | 618 | echo " -s : Start an agent if one is not running" |
614 | echo " -k : Kill all running agents and start new ones" | 619 | echo " -k : Kill all running agents and start new ones" |
615 | echo " -l : Load all known keys into agent" | 620 | echo " -l : Load all known keys into agent" |
@@ -619,30 +624,42 @@ function get_ssh_agent | |||
619 | esac | 624 | esac |
620 | done | 625 | done |
621 | 626 | ||
622 | local agent_out="$HOME/.ssh/agent.out-$(hostname)" | 627 | for path in "${TMPDIR:-/tmp}"/ssh-*/agent.*; do |
623 | 628 | basepath=$(basename $path) | |
624 | # Check if agent is already started. This often happens early in | 629 | sockpath=$(dirname $path) |
625 | # the startup process for X11 (see /etc/X11/xinit/xinitrc-common). | 630 | |
626 | # If the agent was already started then our local state file is | 631 | export SSH_AUTH_SOCK=${path} |
627 | # probably out of date with our environment file, update it so that | 632 | export SSH_AGENT_PID=${basepath##agent.} |
628 | # other sessions in other shells that may not be connected to this | 633 | |
629 | # X11 session use the same agent. | 634 | # When no files are present matching the pattern the pattern itself is |
630 | if _ssh_agent_is_running; then | 635 | # passed through. shopt -s nullglob would fix this but too much |
631 | echo "SSH_AUTH_SOCK=${SSH_AUTH_SOCK}; export SSH_AUTH_SOCK;" > "$agent_out" | 636 | # fiddling to make it work without polluting the parent shell state. |
632 | echo "SSH_AGENT_PID=${SSH_AGENT_PID}; export SSH_AGENT_PID;" >> "$agent_out" | 637 | if [[ "$SSH_AGENT_PID" == "*" ]]; then |
633 | echo "echo Agent pid ${SSH_AGENT_PID};" >> "$agent_out" | 638 | [[ -n "$debug" ]] && echo "No agents found" |
634 | fi | 639 | break |
640 | fi | ||
635 | 641 | ||
636 | source "$agent_out" &>/dev/null | 642 | [[ -n "$debug" ]] && echo -n "Trying ${SSH_AUTH_SOCK} for ${SSH_AGENT_PID}... " |
643 | |||
644 | if _ssh_agent_is_running; then | ||
645 | [[ -n "$debug" ]] && echo "Success" | ||
646 | break | ||
647 | else | ||
648 | [[ -n "$debug" ]] && echo "Failure... removing ${sockpath}" | ||
649 | rm -rf "$sockpath" | ||
650 | fi | ||
651 | done | ||
637 | 652 | ||
638 | # If the agent still isn't connected after sourcing our local agent | 653 | # If the agent still isn't connected after sourcing our local agent |
639 | # state file then it probably isn't running at all. Start a new | 654 | # state file then it probably isn't running at all. Start a new |
640 | # agent and update the state file. | 655 | # agent and update the state file. |
641 | if ! _ssh_agent_is_running && [[ -n "$start_agent" ]]; then | 656 | if ! _ssh_agent_is_running && [[ -n "$start_agent" ]]; then |
642 | eval $(ssh-agent | tee "$agent_out") &>/dev/null | 657 | [[ -n "$debug" ]] && echo "Starting new agent..." |
658 | eval $(ssh-agent) &>/dev/null | ||
643 | fi | 659 | fi |
644 | 660 | ||
645 | if [[ -n "$load_keys" ]]; then | 661 | if [[ -n "$load_keys" ]]; then |
662 | [[ -n "$debug" ]] && echo "Adding SSH keys..." | ||
646 | add_ssh_keys | 663 | add_ssh_keys |
647 | fi | 664 | fi |
648 | } | 665 | } |