Skip to content

Instantly share code, notes, and snippets.

@bradfeehan
Created December 24, 2014 07:31
Show Gist options
  • Save bradfeehan/c3406a2cf0e067fd4c54 to your computer and use it in GitHub Desktop.
Save bradfeehan/c3406a2cf0e067fd4c54 to your computer and use it in GitHub Desktop.
Auto-detect the current user's SSH agent
#!/bin/bash
#
# Sets environment to automatically find the current user's SSH agent
# Echoes to stdout the PID of the process with a particular command line
function pid_of_command {
local command="$1"
ps -u "$(whoami)" \
| grep "$command" \
| grep -v grep \
| awk '{print $2}' \
| head -n 1
}
# The PID of the current user's SSH agent
function ssh_agent_pid {
pid_of_command ssh-agent
}
# Lists the path to the SSH auth file for the current SSH agent
function ssh_agent_socket {
lsof -p "$(ssh_agent_pid)" \
| grep 'unix' \
| awk '{print $8}'
}
function main {
export SSH_AUTH_SOCK="$(ssh_agent_socket)"
export SSH_AGENT_PID="$(ssh_agent_pid)"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment