This web site has been retired. Please follow my activities at pztrick.com.

pZtrick.com

the personal site of patrick paul

Add an Inforc to Your Linux Dotfiles or Bashrc

| Comments

I use a dotfiles repo to maintain my Linux profile settings across multiple machines (e.g. .bashrc and .vimrc and .gitconfig). Every so often, I manage to break one of my remote hosts’ SSH agent for passwordless authentication (after I unlock the PGP keys locally with a password on boot) – and all of a sudden I can’t run my deployment and git scripts on the remote host. (Because they can’t do keypair authorization with github.com et al).

So… I sought to add a brief message about whether SSH agent was running (and which key it had loaded). I routinely edit my .localrc to print pertinent server information upon remoting into a Linux server, but as it would be duplicative to add such info about my SSH agent to all servers’ .localrc I packed it into my dotfiles repo. And then I separated it out as an inforc file to keep my repo neat and clean.

The following code should print the loaded SSH key’s fingerprint when you log in to a remote host (or a warning if there is no key in SSH agent!). I also included a message which prints the most recent dotfiles commit since some hosts’ repo folders stagnate and fall behind the master branch tip.

>> .files/bashrc
1
2
3
4
# Print information to terminal when logging in
if [ -f ~/.files/inforc ]; then
    . ~/.files/inforc
fi
>> .files/inforc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# If not running interactively, don't do anything
[ -z "$PS1" ] && return

echo  # blank line

# echo last git commit
PWD_PATH=$(pwd)
GIT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $GIT_PATH
LC_MSG="$(git log -n1 --format='%s')"
LC_DATE="$(date -d @$(git log -n1 --format='%at') '+%Y-%m-%d %H:%M')"
LC_HASH="$(git log -n1 --format='%h')"
echo -e "\e[34m\e[1m * * * \e[0m[pztrick/dotfiles.git] \e[4m$LC_DATE\e[0m \e[1m\e[94m$LC_MSG\e[0m ($LC_HASH)"
cd $PWD_PATH

# echo SSH key
SSHKEY="$(ssh-add -l 2>&1)"
if [ "$SSHKEY" == "Could not open a connection to your authentication agent." ] || [ "$SSHKEY" == "The agent has no identities." ];
then
    echo -e "\e[31m\e[1m * * * \e[0mNO SSH KEY LOADED \e[31m\e[1m * * * \e[0m"
else
    echo -e "\e[93m\e[1m * * * \e[0mSSH Key: \e[0m\e[92m$SSHKEY\e[0m"
fi

echo  # blank line

Comments