Skip to content

Instantly share code, notes, and snippets.

@Argus-Khan
Last active May 19, 2025 10:40
Show Gist options
  • Save Argus-Khan/e69337aaaad74c4a7fd73ef2b42a181e to your computer and use it in GitHub Desktop.
Save Argus-Khan/e69337aaaad74c4a7fd73ef2b42a181e to your computer and use it in GitHub Desktop.
A simple shell script to initiate ssh agent and add ssh keys at the start of shell. It also provides convenient functions to create and remove ssh keys.
#!/usr/bin/env bash
# Author: Argus K (https://github.com/Argus-Khan)
# Feel free to hit me up ( 0 u 0 )/
# SET PATHS based on your ssh dir configs
SSH_PRV_DIR="$HOME/.secrets";
SSH_PUB_DIR="$HOME/.ssh";
SSH_AGENT_VARIABLES="$HOME/.cache/ssh-agent-init";
if ! [ -d "$HOME/.cache/" ]; then
mkdir "$HOME/.cache/"
fi
if ! [ -d "$SSH_PUB_DIR" ]; then
mkdir "$SSH_PUB_DIR"
fi
if ! [ -d "$SSH_PRV_DIR" ]; then
mkdir "$SSH_PRV_DIR"
fi
if ! [ "$(pidof ssh-agent)" ]; then
ssh-agent -s > "$SSH_AGENT_VARIABLES"
fi
source "$SSH_AGENT_VARIABLES" > /dev/null 2>&1
if [ -n "$(ls -A $SSH_PRV_DIR)" ]; then
for file in "$SSH_PRV_DIR"/*; do
ssh-add "$file" > /dev/null 2>&1
done
fi
# Generates ssh key pair, moves them to respective dirs and copies the public key to clipbaord.
keygen(){
if [ $# -gt 1 ];then
echo -e "\033[0;31m Err:\033[0m Too many arguments, expected 1 given $#."
return 1;
fi
ssh-keygen -q -N '' -t ed25519 -f "$SSH_PUB_DIR/$1"
wl-copy < "$SSH_PUB_DIR/$1.pub" || xclip -sel clip < "$SSH_PUB_DIR/$1.pub"
echo "Public key copied."
mv "$SSH_PUB_DIR/$1" "$SSH_PRV_DIR/"
ssh-add "$SSH_PRV_DIR/$1"
}
# Removes SSH keys, only takes the name of the key file.
keyrm(){
if [ $# -gt 1 ];then
echo -e "\033[0;31m Err:\033[0m Too many arguments, expected 1 given $#."
return 1;
fi
if [ -f "$SSH_PRV_DIR/$1" ]; then
echo -e "Removing keys:\n$SSH_PUB_DIR/$1.pub\n$SSH_PRV_DIR/$1"
rm -i "$SSH_PRV_DIR/$1" "$SSH_PUB_DIR/$1.pub"
else
echo -e "\033[0;31m Err:\033[0m Key not found."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment