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.
Enjoy!
# SSH Agent
# Note: ~/.ssh/environment should not be used, as it
# already has a different purpose in SSH.
env=~/.ssh/agent.env
# 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 ]
else
false
fi
}
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
agent_load_env
fi
# if your keys are not stored in ~/.ssh/id_rsa.pub or ~/.ssh/id_dsa.pub, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
agent_start
add_all_keys
elif ! agent_has_keys; then
add_all_keys
fi
echo `ssh-add -l | wc -l` SSH keys registered.
unset env
7th of August, 2014