Skip to content

Instantly share code, notes, and snippets.

@Eihen
Last active August 17, 2018 23:10
Show Gist options
  • Save Eihen/7fececcfe9abfb55bddfcc8bf99e37c1 to your computer and use it in GitHub Desktop.
Save Eihen/7fececcfe9abfb55bddfcc8bf99e37c1 to your computer and use it in GitHub Desktop.
Git Bash for Windows script for SSH Agent and Keys management
# REQUIREMENTS
# 1) Understanding that this script is provided as-is and I have no responsibility for whatever damage occurs
# That said, if the script was strictly tested and no problems should arise if you use it without any modifications
# of areas which not states that can be modified
# Also if you find any problems let me know to see if I can help you (and/or fix the script)
# Put this at the end of your ~/.bashrc if you already have one or just throw it in your home directory if you don't
# Make sure there are no other scripts starting ssh-agents or adding keys
# This assumes you have https://github.com/magicmonty/bash-git-prompt in your home directory
# If it's in another directory just change the "source $DIRECTORY" line accordingly
# Enable only on git repositories
GIT_PROMPT_ONLY_IN_REPO=1
# Source the gitprompt file
source ~/bash-git-prompt/gitprompt.sh
# Check if there's any running instance of ssh-agent for the current user
if ! ps -u $(whoami) | grep 'ssh-agent' | grep -oE '^\s*\S*' > /dev/null; then
# If no instance is found
# Start ssh-agent and dump output to .ssh-agent-thing
ssh-agent > ~/.ssh-agent-thing
# Eval the dumped agent for the current bash
eval "$(<~/.ssh-agent-thing)"
# Add the keys from ~/.ssh ending with .key since the ssh-agent was just started
ssh-add $HOME/.ssh/*.key
# If there's a running instance check if it was already evaled for the current bash
elif [[ "$SSH_AGENT_PID" == "" ]]; then
# If no ssh-agent was evaled
# Eval the existing ssh-agent instance for the current bash
eval "$(<~/.ssh-agent-thing)"
fi
@Eihen
Copy link
Author

Eihen commented Feb 8, 2018

Known Issues:

  • If you just exit the script with "CTRL+c" or similar without providing the passwords for one or more keys those keys will not be added and will not be prompted to the next time you open the terminal unless you manually kill the running SSH Agent. You can use ssh-add to add those keys without any issue.

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