Created
December 28, 2012 19:00
-
-
Save timball/4400854 to your computer and use it in GitHub Desktop.
ssh_attach is to be sourced from a .profile and connects your shell to previously running ssh-agents . think of it as a slightly functional keymanager for your ssh sessions . --timball
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 | |
# | |
# ssh_attach.sh | |
# | |
# source this file in a .bashrc . | |
# | |
# something like : | |
# | |
# # interactive shell ?? | |
# if [ "$PS1" ]; then | |
# # attaching to a already running ssh-agent | |
# if [ -f "$HOME/lib/ssh_attach.sh" ]; then | |
# source "$HOME/lib/ssh_attach.sh" | |
# fi | |
# fi | |
# | |
# and this script will find running ssh-agent processes and attach to it | |
# | |
# it also gives you a set of aliases that are useful | |
# | |
# Fri Dec 28 11:02:08 EST 2012 | |
# [email protected] | |
# | |
# $Id$ | |
# | |
# some useful aliases | |
alias ssh\-off="eval \`ssh-agent -k\`" | |
alias ssh\-on="eval \`ssh-agent -s\` && ssh-add" | |
AGENT="ssh-agent" | |
hug_ssh () { | |
# getting here means we know that ssh-agent is running | |
# if multiple $AGENTS are running it'll only grab the oldest process (as dictated by tail) | |
# that assumes that the 2nd (or higher) $AGENT processes have been created by the user for a purpose for a reason beyond this script | |
# be sure we're only called once | |
unset hug_ssh | |
# i guess bad things would happen if $AGENT is not unique in the process namespace | |
SSH_AGENT_PID=$(ps -axcu $USER | grep -v grep | grep $AGENT | tail -n 1 | awk '{print $2}') | |
# oh man this is ghetto and fragile ... must find best way to find the SSH_AUTH_SOCK | |
case $(uname -s) in | |
Darwin) | |
SSH_AUTH_SOCK=$(lsof -p $SSH_AGENT_PID | grep --color=no -E $((SSH_AGENT_PID - 1))$ | awk '{print $NF}') | |
;; | |
Linux) | |
# when did lsof stop working in fucking linux ? | |
SSH_AUTH_SOCK=SSH_AUTH_SOCK=$(ls -la /tmp/ssh-*/agent.$((SSH_AGENT_PID - 1)) | awk '{print $NF}') | |
;; | |
*) | |
SSH_AUTH_SOCK=$(lsof -p $SSH_AGENT_PID | grep --color=no -E $((SSH_AGENT_PID - 1))$ | awk '{print $NF}') | |
;; | |
esac | |
# maybe the subshell failed | |
if [[ $? = 1 ]] | |
then | |
interactive_print_err "no socket to attach too !" | |
# probably should kill the ssh-agent process too if we can't find the socket | |
kill $SSH_AGENT_PID | |
unset AGENT | |
interactive_print "turn $AGENT on w/ 'ssh-on'" | |
return 1 | |
fi | |
export SSH_AGENT_PID SSH_AUTH_SOCK &> /dev/null | |
interactive_print "$AGENT attached at $SSH_AGENT_PID $SSH_AUTH_SOCK" | |
return 0 | |
} | |
is_running () { | |
# this tests that $1 is a process that $USER is running | |
# doesn't test to see if $1 is running more than once just that at least one copy is running | |
# use like this : | |
# if [[ "$(is_running $AGENT)" = "0" ]] | |
# then | |
# # good | |
# else | |
# # bad | |
# fi | |
if [ -z "$1" ] | |
then | |
interactive_print_err "bad juju . no program to test against" | |
else | |
ps -xu $USER | grep -v grep | grep $1 &> /dev/null | |
if [ $? = "0" ] | |
then | |
# good | |
echo 0 | |
else | |
# bad | |
echo 1 | |
fi | |
fi | |
} | |
_interactive_print () { | |
if [ ! -z "$PS1" ] | |
then | |
echo $@ | |
fi | |
} | |
interactive_print_err () { | |
if [ ! -z "$PS1" ] | |
then | |
echo $@ >2 | |
fi | |
} | |
interactive_print () { | |
# only print to screen if in interactive prompt | |
# but call it from the protected function | |
if [ ! -z "$1" ] | |
then | |
_interactive_print $@ | |
fi | |
} | |
ssh_agent_attach () { | |
# main function | |
# tests that ssh-agent is running | |
# if it is attach to previously running process ... | |
# be sure we're only called once | |
unset ssh_agent_attach | |
if [[ "$(is_running $AGENT)" = "0" ]] | |
then | |
# good | |
hug_ssh | |
else | |
# bad | |
interactive_print "turn $AGENT on w/ 'ssh-on'" | |
fi | |
return 0 | |
} | |
ssh_agent_attach | |
unset interactive_print | |
unset _interactive_print | |
unset AGENT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment