Skip to content

Instantly share code, notes, and snippets.

@heartonbit
Created December 23, 2024 11:14
Show Gist options
  • Save heartonbit/80cbceee10b71d3a44290844af3b7787 to your computer and use it in GitHub Desktop.
Save heartonbit/80cbceee10b71d3a44290844af3b7787 to your computer and use it in GitHub Desktop.
Script to generate SSH key for GitHub
#! /bin/bash
# Script to generate SSH key for GitHub
# Usage: ./sshkey.sh <email>
# Set colors for output
declare -A colors=(
["red"]='\e[31m'
["green"]='\e[32m'
["cyan"]='\e[36m'
["yellow"]='\e[33m'
["reset"]='\e[0m'
)
# Function to print colored messages
print_message() {
local color=$1
local message=$2
echo -e "${colors[$color]}$message${colors[reset]}"
}
# Function to generate and setup SSH key
setup_ssh_key() {
local email=$1
local key_file="$HOME/.ssh/id_ed25519"
print_message "cyan" "Generating SSH key..."
# Use -f to specify the file path and avoid the prompt
ssh-keygen -q -t ed25519 -C "$email" -N "" -f "$key_file"
print_message "cyan" "Starting SSH agent..."
eval "$(ssh-agent -s)"
print_message "cyan" "Adding SSH key to agent..."
ssh-add "$key_file"
print_message "green" "Please add the following key to your GitHub account:"
cat "$key_file.pub"
}
# Check if email argument is provided
if [ $# -eq 0 ]; then
print_message "red" "Error: Please provide an email address as an argument"
print_message "yellow" "Usage: $0 <email>"
exit 1
fi
# Main execution
setup_ssh_key "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment