Skip to content

Instantly share code, notes, and snippets.

@spongeyperson
Last active April 29, 2023 02:42
Show Gist options
  • Save spongeyperson/df43f760f6f62042bf3e3a5007fc5529 to your computer and use it in GitHub Desktop.
Save spongeyperson/df43f760f6f62042bf3e3a5007fc5529 to your computer and use it in GitHub Desktop.
Spongey's Shitty Shell Script to Automate Creating SSH Identities
#!/bin/bash
#Greeting
echo -e "\e[0m"
echo -e "\e[96m##################################"
echo -e "\e[96m# Welcome to \e[93mSpongey's\e[0m \e[96m#\e[0m"
echo -e "\e[96m# \e[4mNo Excuses\e[0m \e[96mSSH Security Script #\e[0m"
echo -e "\e[96m# \e[31mrev. 4.1\e[0m \e[96m#\e[0m"
echo -e "\e[96m##################################"
# Reset, then ask user to continue script
echo -e "\e[0m"
echo -e "\e[34m::\e[37m Do you want to configure SSH identities? (y/N)\e[0m"
echo -e "\e[32m==> \e[0m\c\r"; read choice
case "$choice" in
y|Y|yes|YES ) choice="y";;
* ) choice="n";;
esac
# Ask user for Username, IP Address, Port Number, and Hostname of SSH Identities
if [ "$choice" = "y" ]; then
echo -e "\e[34m::\e[37m Enter Username: \e[0m"
echo -e "\e[32m==> \e[0m\c\r"; read username
echo -e "\e[34m::\e[37m Enter IP Address: \e[0m"
echo -e "\e[32m==> \e[0m\c\r"; read ip_address
echo -e "\e[34m::\e[37m Enter Port: \e[0m"
echo -e "\e[32m==> \e[0m\c\r"; read port_number
echo -e "\e[34m::\e[37m Enter Hostname: \e[0m"
echo -e "\e[34m::\e[31m\e[5m \e[7mDO NOT\e[0m\e[31m append .local"
echo -e "\e[32m==> \e[0m\c\r"; read hostname
hostname="${hostname%.local}"
# Prompt user to create ssh-identities directory if it doesn't exist
ssh_identities_dir=~/.ssh/ssh-identities
if [ ! -d "$ssh_identities_dir" ]; then
echo -e "\e[34m::\e[37m $ssh_identities_dir does not exist. Do you want to create it? (y/N)\e[0m"
echo -e "\e[32m==> \e[0m\c\r"; read create_ssh_identities_dir
case "$create_ssh_identities_dir" in
y|Y|yes|YES ) mkdir -p "$ssh_identities_dir";;
* ) echo -e "\e[31m❌ \e[0mSSH identity configuration skipped.\e[0m"; exit 1;;
esac
fi
# Create SSH identity files
private_key=~/.ssh/ssh-identities/$hostname
public_key="$private_key.pub"
ssh-keygen -t rsa -b 4096 -f "$private_key" -C "$username@$hostname" -N ""
echo -e "\e[32m==✅ \e[0mSSH identity of \e[5m\e[4mhostname\e[0m created.\e[0m"
echo -e " \e[31mPrivate Key Location:\e[0m $private_key"
echo -e " \e[32mPublic Key Location:\e[0m $public_key"
# Copy identity file to remote host
echo -e "\e[32m==\e[0m"
ssh-copy-id -i "$public_key" "$username@$ip_address" -p "$port_number"
echo -e "\e[32m==✅ \e[0mSSH Public Key Copied to \e[4m$hostname"
# Append host configuration to ~/.ssh/config
printf "Host $hostname\n\
Hostname $ip_address\n\
Port $port_number\n\
IdentityFile $private_key\n\
User $username\n" >> ~/.ssh/config
echo -e "\e[32m==✅ \e[0mSSH identity configuration complete.\e[0m"
echo -e "\e[34m:: \e[0mYou can now SSH using:"
echo -e "\e[0m"
echo -e "\e[34m==> \e[0m\e[4mssh $hostname \e[0m\e[34m<==\e[0m"
echo -e "\e[0m"
else
echo -e "\e[31m❌ \e[0mSSH identity configuration skipped.\e[0m"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment