Skip to content

Instantly share code, notes, and snippets.

@chsxf
Last active April 26, 2021 08:45
Show Gist options
  • Save chsxf/0a1912aca6bc13568efae9f7ba807b0f to your computer and use it in GitHub Desktop.
Save chsxf/0a1912aca6bc13568efae9f7ba807b0f to your computer and use it in GitHub Desktop.
Add all SSH keys at once

Purpose

This script allows you to add all SSH keys from your ~/.ssh folder to the SSH agent at once. It is especially useful when you have several accounts tied to the SSH for the same domain.

Requirements

The script will look for SSH keys in ~/.ssh

You'll also need bash and ssh-add (from OpenSSH or equivalent) installed on your system.

Installation

From the Terminal, move to the directory you want to install this script (I recommend a directory with your $PATH environment variable) and execute those commands:

wget https://gist.githubusercontent.com/chsxf/0a1912aca6bc13568efae9f7ba807b0f/raw/ssh-add-all-keys.sh
chmod +x ssh-add-all-keys.sh
# Color ANSI escape codes:
# https://stackoverflow.com/a/5947802
WHITE='\033[1;37m'
RESET_COLOR='\033[0m'
cd ~/.ssh
KEYS=`ls -1 *.pub`
COUNT_KEYS=`echo "$KEYS" | wc -l | xargs`
echo "Adding $COUNT_KEYS keys...\n"
for entry in $KEYS
do
if [[ $entry =~ ^(.+)\.pub$ ]]; then
echo "${WHITE}Adding '${BASH_REMATCH[1]}' key${RESET_COLOR}"
`ssh-add ${BASH_REMATCH[1]}`
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment