Created
December 24, 2014 07:31
-
-
Save bradfeehan/c3406a2cf0e067fd4c54 to your computer and use it in GitHub Desktop.
Auto-detect the current user's SSH agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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