Skip to content

Instantly share code, notes, and snippets.

@qtfkwk
Created August 27, 2021 12:43
Show Gist options
  • Save qtfkwk/a6e44d8c5879ccde8f14d6ce67f4e5c7 to your computer and use it in GitHub Desktop.
Save qtfkwk/a6e44d8c5879ccde8f14d6ce67f4e5c7 to your computer and use it in GitHub Desktop.
Start SSH agent and/or add keys (add to your .bashrc or .zshrc)
# Start the SSH agent (not needed if desktop environment starts it)
SSH_ENV="$HOME/.ssh/agent"
function start_agent {
MASK=$(umask)
umask 0077
/usr/bin/ssh-agent |sed 's/^echo/#echo/' >"${SSH_ENV}"
umask $MASK
. "${SSH_ENV}" >/dev/null
}
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" >/dev/null
ps -ef |grep ${SSH_AGENT_PID} |grep ssh-agent$ >/dev/null || { start_agent; }
# start if agent file exists but is stale
else
start_agent;
# start if agent file doesn't exist
fi
# Add keys (replace `key1 key2 keyN` with your actual keys)
ssh_keys=$(ssh-add -l)
for i in key1 key2 keyN; do
fp="$(ssh-keygen -lf "$HOME/.ssh/$i")"
if ! echo "$ssh_keys" |grep -F "$fp" >/dev/null; then ssh-add "$HOME/.ssh/$i"; fi
done
# Print keys (optional)
ssh_keys=$(ssh-add -l)
if ! echo "$ssh_keys" |grep -F 'The agent has no identities' >/dev/null; then
echo "\n\x1b[38;2;44;44;44m$(echo $ssh_keys |sed 's/^/* \`/' |sed 's/$/\`/')\x1b[0m"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment