Difference between revisions of "Ssh agent bashrc amendment"

From Wiki at Neela Nurseries
Jump to: navigation, search
m (adding link to Jon Cairn's blog post regarding ssh-agent and ssh-add behaviors and needs.)
m (^ 2021 Q3 Amended ssh-agent Helper Script)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
 +
Keywords:  [[#nn_anchor_ssh_config|ssh config file syntax and reference]]
 +
 +
 +
__TOC__
  
 
2021-09-21 Update:
 
2021-09-21 Update:
Line 11: Line 16:
  
 
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.
 
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.
 +
 +
<!-- comment -->
 +
 +
== [[#top|^]] Bash Run Time Amendment For ssh-agent Use ==
  
 
<i>Script excerpt - bash script excerpt not a stand-alone script:</i>
 
<i>Script excerpt - bash script excerpt not a stand-alone script:</i>
Line 111: Line 120:
  
 
<!-- comment -->
 
<!-- comment -->
 +
 +
== [[#top|^]] Helpful Development Shell Commands ==
 +
 +
Shell command to terminate all ssh-agent daemons running for a particular user:
 +
<pre>
 +
$ for pid in `ps -u $USERNAME | grep ssh-agent | awk '{print $1}'`; do echo $pid; kill $pid; done
 +
</pre>
 +
 +
<!-- comment -->
 +
 +
== [[#top|^]] 2021 Q3 Amended ssh-agent Helper Script ==
 +
 +
Shell script to start and or determine ssh-agent socket file and PID:
 +
 +
<pre>
 +
## 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
 +
</pre>
 +
 +
Shell script amendment to add to .bashrc or dot-bashrc-amendments.sh:
 +
 +
<pre>
 +
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
 +
</pre>
 +
 +
<!-- comment -->
 +
 +
== [[#top|^]] Sample Output ==
 +
 +
<pre>
 +
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-88-generic x86_64)
 +
 +
* Documentation:  https://help.ubuntu.com
 +
* Management:    https://landscape.canonical.com
 +
* Support:        https://ubuntu.com/advantage
 +
 +
  System information as of Thu 14 Oct 2021 07:11:20 PM UTC
 +
 +
  System load:    0.08              Processes:              120
 +
  Usage of /home: 82.9% of 12.93GB  Users logged in:        0
 +
  Memory usage:  5%                IPv4 address for enp0s3: 10.0.2.15
 +
  Swap usage:    0%
 +
 +
 +
6 updates can be applied immediately.
 +
1 of these updates is a standard security update.
 +
To see these additional updates run: apt list --upgradable
 +
 +
 +
Last login: Wed Oct 13 17:49:21 2021 from 10.0.2.2
 +
starting,
 +
script called without bookmarked paths group specified,
 +
looking for last-used bookmarks group in dot-bash-amendments run-time config file . . .
 +
- DEV - from rc file read bookmarks group id '4',
 +
calling 'read directory bookmarks file' with arguments '-bash 4' . . .
 +
caller requests valid bookmarks file identified by '4', which is in the range 1..9
 +
will read bookmarks from file named bookmarked-paths-04.txt,
 +
sourcing script in current dir to capture ssh-agent details...
 +
SSH_AGENT_PID=1285 <--JUST PARSED
 +
Exporting SSH_AUTH_SOCK=/tmp/ssh-jeddQUQFS474/agent.1283 and  SSH_AGENT_PID=1285
 +
done.
 +
guest@vm:~$
 +
</pre>
 +
 +
<!-- comment -->
 +
 +
== [[#top|^]] References ==
 +
 +
*  https://www.ssh.com/academy/ssh/add
 +
<span id="nn_anchor_ssh_config"></span>
 +
*  https://linuxize.com/post/using-the-ssh-config-file/
 +
 +
 +
<!-- command -->

Latest revision as of 19:15, 14 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


^ Sample Output

Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-88-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu 14 Oct 2021 07:11:20 PM UTC

  System load:    0.08               Processes:               120
  Usage of /home: 82.9% of 12.93GB   Users logged in:         0
  Memory usage:   5%                 IPv4 address for enp0s3: 10.0.2.15
  Swap usage:     0%


6 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable


Last login: Wed Oct 13 17:49:21 2021 from 10.0.2.2
starting,
script called without bookmarked paths group specified,
looking for last-used bookmarks group in dot-bash-amendments run-time config file . . .
- DEV - from rc file read bookmarks group id '4',
calling 'read directory bookmarks file' with arguments '-bash 4' . . .
caller requests valid bookmarks file identified by '4', which is in the range 1..9
will read bookmarks from file named bookmarked-paths-04.txt,
sourcing script in current dir to capture ssh-agent details...
 SSH_AGENT_PID=1285 <--JUST PARSED
Exporting SSH_AUTH_SOCK=/tmp/ssh-jeddQUQFS474/agent.1283 and  SSH_AGENT_PID=1285
done.
guest@vm:~$


^ References