Ssh agent bashrc amendment

From Wiki at Neela Nurseries
Revision as of 17:22, 21 September 2021 by Ted (talk | contribs) (adding helpful shell commands used in development of ssh-agent and ssh-add support in Gitbash.)
Jump to: navigation, search

2021-09-21 Update:

Some good insights found here at Jon Cairn's blog. This info may likely simplify Ted's bash run time shell amendments script:

 *  http://blog.joncairns.com/2013/12/understanding-ssh-agent-and-ssh-add/

2020 Overview:

The following bash script snippet is part of a larger "dot bash run-time config" amendments script, written and utilized by Ted Havelka over the years from about 2006 to 2021. This snippet is geared toward amending one or multiple shell instances in a Gitbash environment, in which ssh-agent is used to hold one or more SSL keys for remote git access. The snippet here assumes that the user's given ssh-agent has already had those key pairs successfully added to its local sense and stock of SSL keys.

Some good instructions regarding how to create and to add keys to ssh-agent were found by Ted on 2021-01-13 WED at https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent.


^ Bash Run Time Amendment For ssh-agent Use

Script excerpt - bash script excerpt not a stand-alone script:

# 2020-10-30 FRI - work to configure ssh-agent per gitbash session:

LOCAL_VAR_SSH_AGENT_RUNNING_PROCESS_COUNT=$(ps -u $USERNAME | grep agent | wc | awk '{print $1}')
LOCAL_VAR_SSH_AGENT_VARS_FILENAME="z--ssh-agent-env-vars.txt"

echo "Checking for ssh-agent daemon process:"

if [ $LOCAL_VAR_SSH_AGENT_RUNNING_PROCESS_COUNT -ne 0 ]; then
    echo "Found one or more ssh-agent processes running,"
    if [ -e $LOCAL_VAR_SSH_AGENT_VARS_FILENAME ]; then
        echo "Reading env variables relating to earliest started ssh-agent instance..."
        var1=$(cat $LOCAL_VAR_SSH_AGENT_VARS_FILENAME | sed -n '1p')
        var2=$(cat $LOCAL_VAR_SSH_AGENT_VARS_FILENAME | sed -n '2p')
        echo "exporting $var1 to SSH_AGENT_PID env var..."
        export SSH_AGENT_PID=$var1
        echo "exporting $var2 to SSH_AUTH_SOCK env var..."
        export SSH_AUTH_SOCK=$var2
    else
        echo "but no locally written environment vars found!"
    fi
else
    echo "none found, starting..."
    eval $(/usr/bin/ssh-agent -s)
    /usr/bin/ssh-add $HOME/.ssh/id-ed25519-key
    var3=$(set | grep SSH_AGENT_PID | cut -d'=' -f 2)
    var4=$(set | grep SSH_AUTH_SOCK | cut -d'=' -f 2)
    echo "var3 holds $var3"
    echo "var4 holds $var4"
    echo "Writing these variables to file for future Gitbash shell instances to read..."
    echo $var3 > $LOCAL_VAR_SSH_AGENT_VARS_FILENAME
    echo $var4 >> $LOCAL_VAR_SSH_AGENT_VARS_FILENAME
fi


echo "done."



# EOF ( end of file )



Example text file holding ssh-agent related variables, for export in subsequent shell instances:

 1 export SSH_AGENT_PID=4372
 2 export SSH_AUTH_SOCK=/tmp/ssh-SeAsAEx0sm7B/agent.4371


example of newer file, effectively same variables

 1 379
 2 /tmp/ssh-joJMd1yeEDuk/agent.378


^ Helpful Development Shell Commands

Shell command to terminate all ssh-agent daemons running for a particular user:

$ for pid in `ps -u $USERNAME | grep ssh-agent | awk '{print $1}'`; do echo $pid; kill $pid; done