kindwolf.org Git repositories xavierg-snippets / master steal-ssh-agent / steal-ssh-agent.sh
master

Tree @master (Download .tar.gz)

steal-ssh-agent.sh @masterraw · history · blame

#!/bin/bash
# "Steal" the ssh-agent, assuming there is only one running and we
# are actually allowed to reach its socket (i.e. we are either root
# or the ssh-agent owner).
ssh_agent_pid=$(pgrep -f "^/usr/bin/ssh-agent")
if [ -z "${ssh_agent_pid}" ]; then
	echo "Unable to find ssh-agent"
	exit 158
fi
ssh_agent_ppid=$(perl -nlE 'say $1 if m#^PPid:\s*(\d+)$#' /proc/${ssh_agent_pid}/status)
export SSH_AGENT_PID="${ssh_agent_pid}"
export SSH_AUTH_SOCK=$(/bin/ls /tmp/ssh-*/agent.${ssh_agent_ppid})
[ $# -gt 0 ] && "$@"