Skip to content

Instantly share code, notes, and snippets.

@axelabs

axelabs/agent.sh Secret

Last active January 23, 2019 16:54
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 axelabs/066351eecfc9349527230d1fa0cb3eae to your computer and use it in GitHub Desktop.
Save axelabs/066351eecfc9349527230d1fa0cb3eae to your computer and use it in GitHub Desktop.
Re-use ssh agent cross logins.
# To use this, source from your proFile.
MYAGENTS=(`pgrep -U $USER -f ^ssh-agent$|sort -n`)
echo "Found ${#MYAGENTS[@]} ssh-agents."
# Let's try to take over the agents, like we do everynight Pinky!
if [[ "${MYAGENTS[@]}" ]];then
KEEPER=${MYAGENTS[0]}
echo KEEPER: $KEEPER
OUTCAST=${MYAGENTS[@]:1}
[[ "$OUTCAST" ]] && { echo "Goodbye agent $OUTCAST"; kill $OUTCAST; }
SSH_AUTH_SOCK=`awk '/tmp\/ssh/ {print $NF}' /proc/$KEEPER/net/unix`
export SSH_AUTH_SOCK;
SSH_AGENT_PID=$KEEPER; export SSH_AGENT_PID;
else
NEWAGENT="`ssh-agent`"
echo $NEWAGENT;
eval $NEWAGENT
fi
ssh-add -l | grep "The agent has no identities" && ssh-add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment