Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function sshagent_findsockets {
- find /tmp/ssh-* -uid $(id -u) -name agent.\* 2>/dev/null
- }
- function sshagent_testsocket {
- if [ ! -x "$(which ssh-add)" ] ; then
- echo "ssh-add is not available; agent testing aborted"
- return 1
- fi
- if [ X"$1" != X ] ; then
- export SSH_AUTH_SOCK=$1
- fi
- if [ X"$SSH_AUTH_SOCK" = X ] ; then
- return 2
- fi
- if [ -S $SSH_AUTH_SOCK ] ; then
- ssh-add -l >/dev/null 2>/dev/null
- if [ $? = 2 ] ; then
- echo "Socket $SSH_AUTH_SOCK is dead! Deleting!"
- rm -f $SSH_AUTH_SOCK
- return 4
- else
- echo "Found ssh-agent $SSH_AUTH_SOCK"
- return 0
- fi
- else
- echo "$SSH_AUTH_SOCK is not writeable!"
- return 3
- fi
- }
- function sshagent_init {
- # ssh agent sockets can be attached to a ssh daemon process or an
- # ssh-agent process.
- AGENTFOUND=0
- # Attempt to find and use the ssh-agent in the current environment
- if sshagent_testsocket ; then AGENTFOUND=1
- # If there is no agent in the environment, search /tmp for
- # possible agents to reuse before starting a fresh ssh-agent
- # process.
- else
- for agentsocket in $(sshagent_findsockets) ; do
- if sshagent_testsocket $agentsocket ; then AGENTFOUND=1 ; break ; fi
- done
- fi
- # If at this point we still haven't located an agent, it's time to
- # start a new one
- if [ $AGENTFOUND = 0 ] ; then
- # eval $(ssh-agent)
- echo agent not found
- else
- # show what keys are currently in the agent
- ssh-add -l
- fi
- # Clean up
- unset AGENTFOUND agentsocket
- }
- sshagent_init
Advertisement
Add Comment
Please, Sign In to add comment