Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Load nfs-mounted ssh keys at login automatically

I use multiple ssh keys across multiple systems. Some systems need to have the same ssh key loaded. My solution is to store the generic ones on my nfs mount, accessible only to my user, and to use a function in .bashrc:

load_ssh_key() {
   test -n "${1}" && SSHKEY="${1}" ;
   test -z "${SSHKEY}" && SSHKEY=/mnt/bgstack15/.ssh/bgstack15_devuan.key
   if test -e "${SSHKEY}" ;
   then
      test -z "${SSH_AGENT_PID}" && eval $( ssh-agent ) | grep -viE 'Agent pid'
      ssh-add "${SSHKEY}" 2>&1 | grep -viE "Identity added:" 1>&2
   else
      echo "Unable to get to private key!" 1>&2
   fi
   unset SSHKEY
}

load_ssh_key

Also, now, the function is generally available for invoking with a filename to load that ssh key. I realize ssh-add is pretty trivial, but I want the function to fail silently for when I'm off-network (when I won't be doing any ssh work anyway).

Comments