-
-
Save jolros/8163107 to your computer and use it in GitHub Desktop.
Shell script that adds keys to ssh-agent. Options to force a re-injection or set a custom lifetime for the keys. It will scan your .ssh directory for private keys or you can specify a key enumeration file. If your keys need passphrases you will add them during the scripts execution. See: http://blog.jolros.com/post/71467614049/revisiting-my-ssh-…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
read -d '' INSTRUCTIONS << EOF | |
Checks that the ssh-agent is running, and if not, kick it off | |
The script will find all private keys in your $HOME/.ssh directory. | |
You can also create a file called $HOME/.ssh/agentkeys containing | |
absolute paths or paths relative to the $HOME/.ssh directory of keys | |
you wish to add (variables will be expanded). | |
Options: | |
-h: Prints usage | |
-r: Restarts ssh-agent and re-injects all the keys | |
-t HOURS: Number of hours to keep the keys active (default 10) | |
EOF | |
KEYTIME="10h" | |
KEYLISTFILE="${HOME}/.ssh/agentkeys" | |
RESTART=0 | |
usage(){ | |
echo "$INSTRUCTIONS" | |
exit 1 | |
} | |
while getopts "hrt:" opt; do | |
case "$opt" in | |
h|\?) usage | |
exit 0 | |
;; | |
r) RESTART=1 | |
;; | |
t) KEYTIME="${OPTARG}h" | |
;; | |
esac | |
done | |
if [ $RESTART -eq 1 ]; then | |
echo "Restarting ssh-agent" | |
killall ssh-agent | |
sleep 1 | |
fi | |
if [[ -z $SSH_AUTH_SOCK ]]; then | |
SOCKETFILE=${HOME}/tmp/ssh/ssh-agent.socket | |
else | |
SOCKETFILE=${SSH_AUTH_SOCK} | |
fi | |
KEYS=() | |
if [[ -e "${KEYLISTFILE}" ]]; then | |
# Keys in the agentkeys file are absolute or relative to the .ssh directory | |
pushd "${HOME}/.ssh/" > /dev/null | |
while read; do | |
eval KEYSTR="$REPLY" | |
case $KEYSTR in | |
/*) KEYSTR=$KEYSTR;; | |
*) KEYSTR=$PWD/$KEYSTR;; | |
esac | |
KEYS+=("$KEYSTR") | |
done < "${KEYLISTFILE}" | |
popd > /dev/null | |
else | |
FOUNDKEYS=$(grep -rl 'BEGIN .* PRIVATE KEY' ${HOME}/.ssh) | |
for KEY in $FOUNDKEYS; do | |
KEYS+=("$KEY") | |
done | |
fi | |
if [ ${#KEYS[@]} -eq 0 ]; then | |
echo "No keys found to add" | |
exit 1 | |
fi | |
ps -wU ${USER} | grep "[s]sh-agent" > /dev/null | |
if [[ $? -gt 0 ]]; then | |
echo "ssh-agent not running" | |
rm -f ${SOCKETFILE} | |
ssh-agent -a ${SOCKETFILE} > /dev/null | |
chmod 600 ${SOCKETFILE} | |
for KEY in "${KEYS[@]}"; do | |
ssh-add -t $KEYTIME "${KEY}" | |
done | |
else | |
ssh-add -l | grep "The agent has no identities" > /dev/null | |
if [[ $? -eq 0 ]]; then | |
echo "ssh-agent running but no keys found. Adding..." | |
for KEY in "${KEYS[@]}"; do | |
ssh-add -t $KEYTIME "${KEY}" | |
done | |
else | |
echo "ssh-agent running and appropriate keys found" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment