Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # init_ssh-agent.sh
- # Ssh agent initialisation script
- #
- # Description:
- # 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.
- # Otherwise this script will run a new ssh-agent and add your id_rsa file to it. And again set the variables needed.
- #
- # Version: 1.0
- # Author: Dieter Castel
- # License: Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
- # See: http://creativecommons.org/licenses/by-nc-sa/3.0/ for more info.
- # Variables:
- # You can change this paths as you wish but they must remain absolute for ssh-agent.
- # WARNING: before changing the paths consider the remove lines: rm -f "$scriptpath" and rm -f -r "$socket".
- #
- # Location of the script that is created when this script first runs.
- scriptpath=~/.ssh/set_ssh-agent.sh
- # Socket you want the agent to run on.
- socket=~/.ssh-sock
- # Location of the keyfile. Change if you want to use an other keyfile.
- keyfile=~/.ssh/id_rsa
- echo "Will initialize ssh-agent for following user: $USER"
- # Function that will initialize a new ssh agent.
- function init_new_ssh_agent {
- echo "No running ssh-agent for your login."
- #No running ssh-agent.
- #Remove old files and directories.
- rm -f "$scriptpath"
- rm -f -r "$socket"
- #Make ssh-agent on the socket and create the script.
- ssh-agent -a "$socket" > "$scriptpath"
- #Run the script to set the environmental variables
- source "$scriptpath"
- #Add the keyfile
- ssh-add "$keyfile"
- #Write protect the file so you don't accidentely remove/change it.
- chmod u-w "$scriptpath"
- echo "ssh-agent (PID = $SSH_AGENT_PID) is ready for use."
- }
- # Get a list of process ids of ssh-agents.
- pidList=`pidof ssh-agent`
- # Test if there is one running already.
- if pidof ssh-agent; then
- #Some agent is already running
- if ssh-add -l | grep "$USER"; then
- #Agent with given login is already up and running.
- echo "ssh-agent (PID = $SSH_AGENT_PID) with your login is already running on socket $SSH_AUTH_SOCK"
- return;
- fi
- #Test if the running ssh-agent has the same PID as our script.
- if [ -e "$scriptpath" ] && [ -e "$socket" ] && source "$scriptpath"; then
- echo "Succesfully ran $scriptpath"
- if ssh-add -l; then
- #Succes the ssh-agent running is functional.
- if ssh-add -l | grep "$USER"; then
- #The key for $login is already added to the ssh-agent.
- echo "ssh-agent (PID = $SSH_AGENT_PID) is ready for use";
- else
- #Adding the keyfile to the agent (prompt for passphrase will show up).
- ssh-add "$keyfile"
- echo "ssh-agent (PID = $SSH_AGENT_PID) is ready for use";
- fi
- fi;
- else
- echo "Failed running $scriptpath ."
- init_new_ssh_agent;
- fi;
- else
- init_new_ssh_agent;
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement