#!/bin/bash # Load any keys in the ssh-keys directory # # Passphrase must be in a variable of the form keyfilename_PASSPHRASE # for example if the file is id_rsa then the variable is id_rsa_PASSPHRASE. # # As such each file must be composed of characters valid in variables. # (No dashes or dots, for example.) # # Ignores any *.pub files. # Are we acting as the askpass script? if [ $# -gt 0 ] ; then read foo echo $foo exit 0 fi set -e set -u me=$(realpath $0) cd $(dirname $0) # Only run if keys present [ -d ./ssh-keys ] || exit 0 # Assure ssh thinks we're on x11. export DISPLAY=:0 eval $(ssh-agent -a /tmp/ssh-agent.sock -s) >/dev/null cd ./ssh-keys for key in * do if echo $key | grep -q '\.pub$' ; then continue fi varname="${key}_PASSPHRASE" passphrase=${!varname} if [ -z "${passphrase}" ] ; then continue fi # Make sure it's only readable by us chmod 600 $key SSH_ASKPASS="${me}" ssh-add "${key}" <<<"${passphrase}" 2>/dev/null || echo "Could not load ssh key $key - Bad passphrase in $varname?" done