Skip to content

Instantly share code, notes, and snippets.

@frntn
Created May 10, 2017 23:01
Show Gist options
  • Save frntn/66fdeb16b5c62b8b53640a2f4a700fae to your computer and use it in GitHub Desktop.
Save frntn/66fdeb16b5c62b8b53640a2f4a700fae to your computer and use it in GitHub Desktop.
#!/bin/bash
cd "$(dirname "$0")"
GIT_KEYS="gitkey_*" # my git keys are always loaded but agent forwarding is limited specific Hosts
SSH_KEYS="" # my ssh keys are never loaded
SSH_AGENT_PID=1 # `kill -0 1` will always fail for non-root
SSH_AGENT_ENV=".agent_env"
[ -f "${SSH_AGENT_ENV}" ] && source "${SSH_AGENT_ENV}" > /dev/null # load latest known agent pid
kill -0 $SSH_AGENT_PID 2>/dev/null && return # and stop here if still running (do not 'exit' because file may be source'd)
echo "Loading SSH Agent..."
chmod -f 600 "${SSH_AGENT_ENV}"
/usr/bin/ssh-agent > "${SSH_AGENT_ENV}" # overwrite latest (not running anymore)
source "${SSH_AGENT_ENV}" > /dev/null
for key in $GIT_KEYS; do /usr/bin/ssh-add "$key"; done
# ...stuff before...
for i in ~/.bash_aliases ~/.ssh/.agent_loader
do
[ -f "$i" ] && source "$i"
done
# ...stuff after...
#!/bin/bash
ssh-keygen -f ~/.ssh/gitkey_git.example.com -P randompass -t ed25519 -C "username@provider.com for git.example.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment