kuldietercastel

init_ssh-agent.sh

Mar 2nd, 2013
53
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # init_ssh-agent.sh
  2. # Ssh agent initialisation script
  3. #
  4. # Description:
  5. #   If there is running an ssh-agent already with your username then $SSH_AGENT_PID en $SSH_AUTH_SOCK will be set to the correct variables.
  6. #   Otherwise this script will run a new ssh-agent and add your id_rsa file to it. And again set the variables needed.
  7. #
  8. # Version:  1.0
  9. # Author:   Dieter Castel
  10. # License:  Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
  11. #           See: http://creativecommons.org/licenses/by-nc-sa/3.0/ for more info.
  12.  
  13. # Variables:
  14. #   You can change this paths as you wish but they must remain absolute for ssh-agent.
  15. #   WARNING: before changing the paths consider the remove lines: rm -f "$scriptpath" and rm -f -r "$socket".
  16. #
  17. #   Location of the script that is created when this script first runs.
  18. scriptpath=~/.ssh/set_ssh-agent.sh
  19. #   Socket you want the agent to run on.
  20. socket=~/.ssh-sock
  21. #   Location of the keyfile. Change if you want to use an other keyfile.
  22. keyfile=~/.ssh/id_rsa
  23.  
  24.  
  25. echo "Will initialize ssh-agent for following user: $USER"
  26.  
  27. # Function that will initialize a new ssh agent.
  28. function init_new_ssh_agent {
  29.     echo "No running ssh-agent for your login."
  30.     #No running ssh-agent.
  31.     #Remove old files and directories.
  32.     rm -f "$scriptpath"
  33.     rm -f -r "$socket"
  34.     #Make ssh-agent on the socket and create the script.
  35.     ssh-agent -a "$socket" > "$scriptpath"
  36.     #Run the script to set the environmental variables
  37.     source "$scriptpath"
  38.     #Add the keyfile
  39.     ssh-add "$keyfile"
  40.     #Write protect the file so you don't accidentely remove/change it.
  41.     chmod u-w "$scriptpath"
  42.     echo "ssh-agent (PID = $SSH_AGENT_PID) is ready for use."
  43. }
  44.  
  45. # Get a list of process ids of ssh-agents.
  46. pidList=`pidof ssh-agent`
  47. # Test if there is one running already.
  48. if pidof ssh-agent; then
  49. #Some agent is already running
  50.     if ssh-add -l | grep "$USER"; then
  51.         #Agent with given login is already up and running.
  52.         echo "ssh-agent (PID = $SSH_AGENT_PID) with your login is already running on socket $SSH_AUTH_SOCK"
  53.         return;
  54.     fi
  55.     #Test if the running ssh-agent has the same PID as our script.
  56.     if [ -e "$scriptpath" ] && [ -e "$socket" ] && source "$scriptpath"; then
  57.         echo "Succesfully ran $scriptpath"
  58.         if ssh-add -l; then
  59.             #Succes the ssh-agent running is functional.
  60.             if ssh-add -l | grep "$USER"; then
  61.                 #The key for $login is already added to the ssh-agent.
  62.                 echo "ssh-agent (PID = $SSH_AGENT_PID) is ready for use";
  63.             else
  64.                 #Adding the keyfile to the agent (prompt for passphrase will show up).
  65.                 ssh-add "$keyfile"
  66.                 echo "ssh-agent (PID = $SSH_AGENT_PID) is ready for use";
  67.             fi
  68.         fi;
  69.     else
  70.         echo "Failed running $scriptpath ."
  71.         init_new_ssh_agent;
  72.     fi;
  73. else
  74.     init_new_ssh_agent;
  75. fi
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×