Skip to content

Instantly share code, notes, and snippets.

@bcomnes
Last active July 5, 2016 20:05
Show Gist options
  • Select an option

  • Save bcomnes/a1f4d262fd98a8002264 to your computer and use it in GitHub Desktop.

Select an option

Save bcomnes/a1f4d262fd98a8002264 to your computer and use it in GitHub Desktop.
Git ssh key encryption password provided via ssh_askpass that integrates with windows credential store
# based on script from here:
# https://help.github.com/articles/working-with-ssh-key-passphrases#platform-windows
# https://github.com/lukesampson/pshazz/blob/41f5c7b832acf862a12cf4630bbe88e6e12f6e6e/plugins/ssh.ps1
# 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_keys() {
export SSH_ASKPASS=~/AppData/Local/scoop/shims/askpass.exe
export DISPLAY=localhost:0.0
/dev/null | ssh-add
}
if ! agent_is_running; then
agent_load_env
fi
# if your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
agent_start
add_keys
elif ! agent_has_keys; then
add_keys
fi
unset env
@bcomnes
Copy link
Copy Markdown
Author

bcomnes commented Oct 10, 2015

Put this .bashrc into %UserProfile%. It's safe to mark it as hidden if you want.

That askpass.exe is special.

https://github.com/lukesampson/askpass

When ssh asks for a password to decrypt the private key, it asks the windows credential store instead of the terminal. If the windows credential store doesn't have it, it prompts the user and caches that password so you never have to type it in again.

Right now the easiest way to install it is to install scoop and pshazz from powershell:

https://github.com/lukesampson/scoop
https://github.com/lukesampson/pshazz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment