Thursday, February 23, 2012

Working with ssh-agent

For checking some git repositories it is very uncomfortable to work with public-keys because you have to commit the key every time you want to push, pull or other communications with the git repository.

If you have a lot of git repositories for a project you have to change to the first directory, make a git pull, commit your password for the public key and go to the next directory. For a lot of directories it is very bad style.

This is were ssh-agent comes to the spot. I start the ssh-agent and commit my password for the public key once with ssh-add. After that you can work with all git repositories were you have the same public key committed. The idea of the ssh-agent is to start it once and then you have to put some environment variables into all your shell where you are working. That's not very nice for me so I wrote a zsh-function for that case. Here you can see the function:
function ssha {
  if [ $# -ne 1 ]; then
    echo "usage: $0 [start|stop]"
    return 1
  fi
  case "$1" in
    start)
      if [ -n "$SSH_AGENT_PID" ]; then
        echo "ssh-agent already running"
        return 1
      fi
      /usr/bin/ssh-agent > /tmp/ssh-agent-pids
      . /tmp/ssh-agent-pids > /dev/null
      /usr/bin/ssh-add
      ;;
    stop)
      if [ -z "$SSH_AGENT_PID" ]; then
        echo "ssh-agent not running"
        return 1
      fi
      /usr/bin/ssh-agent -k > /tmp/ssh-agent-pids
      . /tmp/ssh-agent-pids > /dev/null
      ;;
    *)
      echo "usage: $0 [start|stop]"
      return 1
      ;;
  esac
}
You can start and stop the ssh-agent with the same function. If you are working with some git repositories you could start it with ssha start. Then you have to give your password for the public key and you can work in the same shell. After finishing some work with that git repositories you could stop the server with ssha stop.

No comments:

Post a Comment