Guest User

Untitled

a guest
Feb 6th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.72 KB | None | 0 0
  1. function sshagent_findsockets {
  2.     find /tmp/ssh-* -uid $(id -u) -name agent.\* 2>/dev/null
  3. }
  4.  
  5. function sshagent_testsocket {
  6.     if [ ! -x "$(which ssh-add)" ] ; then
  7.         echo "ssh-add is not available; agent testing aborted"
  8.         return 1
  9.     fi
  10.  
  11.     if [ X"$1" != X ] ; then
  12.         export SSH_AUTH_SOCK=$1
  13.     fi
  14.  
  15.     if [ X"$SSH_AUTH_SOCK" = X ] ; then
  16.         return 2
  17.     fi
  18.  
  19.     if [ -S $SSH_AUTH_SOCK ] ; then
  20.         ssh-add -l >/dev/null 2>/dev/null
  21.         if [ $? = 2 ] ; then
  22.             echo "Socket $SSH_AUTH_SOCK is dead!  Deleting!"
  23.             rm -f $SSH_AUTH_SOCK
  24.             return 4
  25.         else
  26.             echo "Found ssh-agent $SSH_AUTH_SOCK"
  27.             return 0
  28.         fi
  29.     else
  30.         echo "$SSH_AUTH_SOCK is not writeable!"
  31.         return 3
  32.     fi
  33. }
  34.  
  35. function sshagent_init {
  36.     # ssh agent sockets can be attached to a ssh daemon process or an
  37.     # ssh-agent process.
  38.  
  39.     AGENTFOUND=0
  40.  
  41.     # Attempt to find and use the ssh-agent in the current environment
  42.     if sshagent_testsocket ; then AGENTFOUND=1
  43.  
  44.     # If there is no agent in the environment, search /tmp for
  45.     # possible agents to reuse before starting a fresh ssh-agent
  46.     # process.
  47.     else
  48.         for agentsocket in $(sshagent_findsockets) ; do
  49.             if sshagent_testsocket $agentsocket ; then AGENTFOUND=1 ; break ; fi
  50.         done
  51.     fi
  52.  
  53.     # If at this point we still haven't located an agent, it's time to
  54.     # start a new one
  55.     if [ $AGENTFOUND = 0 ] ; then
  56. #        eval $(ssh-agent)
  57.         echo agent not found
  58.     else
  59.         # show what keys are currently in the agent
  60.         ssh-add -l
  61.     fi
  62.  
  63.     # Clean up
  64.     unset AGENTFOUND agentsocket
  65.  
  66. }
  67.  
  68. sshagent_init
Advertisement
Add Comment
Please, Sign In to add comment