#!/bin/sh # Installation: source this from your .bashrc # # Limitations: if $ssh_auth_sock exists and an ssh-agent is running # which doesn't use it, ssh-reclaim will be tricked into thinking # that they correspond. This will happen if the computer shuts down # uncleanly. # # Possible workarounds: # * Check the command line of ssh-agent to see if we started it # * Check for ssh sockets in /tmp owned by $USER # Colors GRAY="\x1b[0;37;01m" RED="\x1b[1;31;01m" GREEN="\x1b[1;32;01m" YELLOW="\x1b[1;33;01m" BLUE="\x1b[1;34;01m" WHITE="\x1b[1;37;01m" DEFAULT="\x1b[0m" [ -x "`which pkill`" ] || return start_agent() { eval `ssh-agent -s -a "$1"` >/dev/null if [ -t 0 ]; then echo -e " ${BLUE}*${DEFAULT} New agent started (pid ${SSH_AGENT_PID})" fi } ssh_agent_pid=`pgrep -u $USER ssh-agent` ssh_auth_sock=/tmp/ssh-agent-auth-sock-$USER if [ -z "$ssh_agent_pid" ]; then rm -f "$ssh_auth_sock" start_agent "$ssh_auth_sock" else if [ -e "$ssh_auth_sock" ]; then export SSH_AUTH_SOCK="$ssh_auth_sock" SSH_AGENT_PID=$ssh_agent_pid else if [ -t 0 ]; then echo -e " ${YELLOW}*${DEFAULT} Killing stale agent (pid $ssh_agent_pid)" fi pkill -u $USER ssh-agent start_agent "$ssh_auth_sock" fi fi