SSH-Agent and Windows

During my day job I use a Windows laptop to make working with a number of .NET projects easier. That said, I've not completely abandoned Node as a platform, nor do I wish to leave Bash behind. Ergo, I wind up using msysgit to emulate a Bash-like environment for Windows. To make things even easier, I use ConEmu to provide a tabbed interface around msysgit. The final piece of the problem is that I have multiple git accounts with their own SSH key (one GitHub for Enterprise, and one Github), and each of these keys has a passphrase.

The naive approach is to just add eval $(ssh-agent -s) to the top of my .bashrc file. The Problem is that this requires a separate ssh-add call for every tab, requiring me to enter my passphrase in every tab. This pretty-well defeats any ease-of-use I was originally looking for.

The Solution

The following is a modified form of GitHub's solution, which surprisingly works perfectly in ConEmu, unchanged. The only change I wanted to make was to automate adding every id_rsa_* SSH key in my ~/.ssh folder.


# SSH Agent
# Note: ~/.ssh/environment should not be used, as it
#       already has a different purpose in SSH.


# Note: Don't bother checking SSH_AGENT_PID. It's not used
#       by SSH itself, and it might even be incorrect
#       (for example, when using agent-forwarding over SSH).

agent_is_running() {
  if [ "$SSH_AUTH_SOCK" ]; then
    # ssh-add returns:
    #   0 = agent running, has keys
    #   1 = agent running, no keys
    #   2 = agent not running
    ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]

agent_has_keys() {
  ssh-add -l >/dev/null 2>&1

agent_load_env() {
  . "$env" >/dev/null

agent_start() {
  (umask 077; ssh-agent >"$env")
  . "$env" >/dev/null

add_all_keys() {
  ls ~/.ssh | grep id_rsa[^.]*$ | sed "s:^:`echo ~`/.ssh/:" | xargs -n 1 ssh-add

if ! agent_is_running; then

# if your keys are not stored in ~/.ssh/ or ~/.ssh/, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
elif ! agent_has_keys; then

echo `ssh-add -l | wc -l` SSH keys registered.

unset env
More Posts