Difference between revisions of "Ssh agent bashrc amendment"

From Wiki at Neela Nurseries
Jump to: navigation, search
m (^ References)
m (adding link to local anchor point, ssh config file reference at linuxize.com.)
Line 1: Line 1:
 +
 +
Keywords:  [[#nn_anchor_ssh_config|ssh config file syntax and reference]]
 +
  
 
__TOC__
 
__TOC__

Revision as of 22:32, 4 October 2021

Keywords: ssh config file syntax and reference


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


^ 2021 Q3 Amended ssh-agent Helper Script

Shell script to start and or determine ssh-agent socket file and PID:

## Filename:  z-configure-ssh-agent-per-gitbash-session.sh
## Helpful development command:
##
## Terminate all running instances of ssh-agent on Windows 10 host running gitbash:
##   $ for pid in `ps -u $USERNAME | grep ssh-agent | awk '{print $1}'`; do echo $pid; kill $pid; done
##
## Obtain path to latest started ssh-agent socket file:
##   $ ssh-agent | grep ^SSH_ | grep SSH_AUTH_ | cut -d ";" -f 1
##
## Append an equals sign to end of ssh-agent socket file path:
##   $ var99=`ssh-agent | grep ^SSH_ | grep SSH_AUTH_ | cut -d ";" -f 1`; var99="$var99="; echo "$var99"
##
##


## Variables used in this script:
LOCAL_VAR_SSH_AGENT_RUNNING_PROCESS_COUNT=$(ps -u $USERNAME | grep agent | wc | awk '{print $1}')
NN__SSH_AGENT_PID_AND_SOCKET_FILE_HELPER_SCRIPT="./z-export-ssh-agent-pid-and-socket-filename.sh"
var99=""
var98=""


## If we find ssh-agent is already running:
if [ $LOCAL_VAR_SSH_AGENT_RUNNING_PROCESS_COUNT -ne 0 ]; then
    echo "Found one or more ssh-agent processes running,"
    if [ -e ./z-export-ssh-agent-pid-and-socket-filename.sh ]; then
        echo "exporting ssh-agent daemon socket file and PID from current dir text file."
        . ./z-export-ssh-agent-pid-and-socket-filename.sh
    fi
    if [ -e ../z-export-ssh-agent-pid-and-socket-filename.sh ]; then
        echo "exporting ssh-agent daemon socket file and PID from parent dir text file."
        . ../z-export-ssh-agent-pid-and-socket-filename.sh
    fi
else
    NN__SOCKET_AND_PID_LINES=`ssh-agent | grep ^SSH_`
## Though ssh-agent outputs a line assigning SSH_AUTH_SOCK with socket filename it is missing a terminating '=',
## handle that here:
    var99=`echo $NN__SOCKET_AND_PID_LINES | grep SSH_AUTH_SOCK | cut -d ";" -f 1`; var99="$var99="
    echo "${var99}" > $NN__SSH_AGENT_PID_AND_SOCKET_FILE_HELPER_SCRIPT
    echo "export SSH_AUTH_SOCK" >> $NN__SSH_AGENT_PID_AND_SOCKET_FILE_HELPER_SCRIPT

## Capture and export present ssh-agent daemon process ID:

## PARSING DEV TEST
#echo "NN__SOCKET_AND_PID_LINES holds:
#${NN__SOCKET_AND_PID_LINES}
#"
## PARSING DEV TEST
#echo "var98 holds:"
#echo "$var98
#"

    var98=`echo $NN__SOCKET_AND_PID_LINES | grep SSH_AGENT_PID | cut -d ";" -f 3`
echo "$var98 <--JUST PARSED"
    echo "$var98" >> $NN__SSH_AGENT_PID_AND_SOCKET_FILE_HELPER_SCRIPT
    echo "export SSH_AGENT_PID" >> $NN__SSH_AGENT_PID_AND_SOCKET_FILE_HELPER_SCRIPT
    export ${var99}
    export $var98

#    echo "Exporting SSH_AUTH_SOCK=${SSH_AUTH_SOCK} and SSH_AGENT_PID=${SSH_AGENT_PID}"
    echo "Exporting ${var99} and ${var98}"
fi

Shell script amendment to add to .bashrc or dot-bashrc-amendments.sh:

if [ -e ./z-configure-ssh-agent-per-gitbash-session.sh ]; then
    echo "sourcing script in current dir to capture ssh-agent details..."
    . ./z-configure-ssh-agent-per-gitbash-session.sh
else
    if [ -e ../z-configure-ssh-agent-per-gitbash-session.sh ]; then
        echo "sourcing script in parent dir to capture ssh-agent details..."
        . ../z-configure-ssh-agent-per-gitbash-session.sh
    fi
fi


^ References