Skip to content

Instantly share code, notes, and snippets.

@ochoarobert1
Created July 11, 2023 12:38
Show Gist options
  • Save ochoarobert1/6d91927c14383deedcb55b4846f58ccb to your computer and use it in GitHub Desktop.
Save ochoarobert1/6d91927c14383deedcb55b4846f58ccb to your computer and use it in GitHub Desktop.
Bash config to call automatically different ssh keys
# Add this on .bashrc
# Start SSH Agent
#----------------------------
SSH_ENV="$HOME/.ssh/environment"
function run_ssh_env {
. "${SSH_ENV}" > /dev/null
}
function start_ssh_agent {
echo "Initializing new SSH agent..."
ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo "SSH Activated"
chmod 600 "${SSH_ENV}"
run_ssh_env;
if [ $PWD = "/folder/to/be/checked" ]; then
echo "Adding Key 1"
ssh-add ~/.ssh/key1;
else
echo "Adding Key 2"
ssh-add ~/.ssh/key2;
fi
}
if [ -f "${SSH_ENV}" ]; then
run_ssh_env;
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_ssh_agent;
}
else
start_ssh_agent;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment