Skip to content

Instantly share code, notes, and snippets.

@shelooks16
Created July 21, 2019 10:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shelooks16/03f6cfd4b86e1be14b83fc8f95e2e72e to your computer and use it in GitHub Desktop.
Save shelooks16/03f6cfd4b86e1be14b83fc8f95e2e72e to your computer and use it in GitHub Desktop.
SSH agent management for windows
function addkey() {
# path to agent file with env vars
env=~/.ssh/agent.env
# path to RSA private key
key=~/id_rsa
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () { (umask 077; ssh-agent >| "$env") && . "$env" >| /dev/null ; }
# ssh-add return value: 0=running w/ key; 1=running w/o key; 2=not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
agent_load_env
if [ -z "$SSH_AUTH_SOCK" ] || [ "$agent_run_state" = "2" ]; then
agent_start
ssh-add $key
echo -e "Hi, put your name here :)"
elif [ "$SSH_AUTH_SOCK" ] && [ "$agent_run_state" = "1" ]; then
ssh-add $key
echo -e "Running without key..."
elif [ "$SSH_AUTH_SOCK" ] && [ "$agent_run_state" = "0" ]; then
echo -e "SSH agent is already running."
fi
unset env
}
function offkey() {
if [ "${SSH_AGENT_PID+1}" == "1" ]; then
ssh-add -D
ssh-agent -k > /dev/null 2>&1
unset SSH_AGENT_PID
unset SSH_AUTH_SOCK
else
echo "There is no ssh agent running."
fi
}
function viewkey() {
ssh-add -L
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment