Skip to content

Instantly share code, notes, and snippets.

@atoa
Created September 25, 2016 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atoa/6ed949b6c1c6c07cacb7d4350c18c0c3 to your computer and use it in GitHub Desktop.
Save atoa/6ed949b6c1c6c07cacb7d4350c18c0c3 to your computer and use it in GitHub Desktop.
ssh-agent helper
#!/bin/bash
# ssh-agent helper script. runs ssh-agent and adds ~/.ssh/*.pem keys
PATH="/bin:/usr/bin"
umask 077
IFS=' '
readonly AGENT_SOCKET="${HOME}/.ssh/.ssh-agent-socket"
readonly AGENT_INFO="${HOME}/.ssh/.ssh-agent-info"
if [[ -s "$AGENT_INFO" ]]
then
source "$AGENT_INFO"
fi
RUN_PID="$(ps -ef | grep '[s]sh-agent' | awk '{print $2}')"
if [[ -z "$SSH_AGENT_PID" || "$SSH_AGENT_PID" != $RUN_PID ]]
then
if [[ -S "$AGENT_SOCKET" ]]
then
echo "[WARN] - socket file exists but agent is not running - removing socket file" >&2
rm -i "$AGENT_SOCKET"
fi
eval $(ssh-agent -s -a "$AGENT_SOCKET")
echo "export SSH_AGENT_PID=${SSH_AGENT_PID}" > "$AGENT_INFO"
echo "export SSH_AUTH_SOCK=${SSH_AUTH_SOCK}" >> "$AGENT_INFO"
for file in $(echo "${HOME}/.ssh/"*.pem)
do
ssh-add "$file"
done
else
echo "[INFO] - agent already running" >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment