Advertisement
Mark2020H

Create ssh keys for git hub All menu driven

May 11th, 2023
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.14 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3.  
  4. ## MD Harrington 11-05-2023  Time 13:45hrs
  5.  
  6. ## Bash script to assist with generating ssh keys for git hub
  7. ## Add your SSH private key to the ssh-agent
  8. ## Copy key to clip board  ready to paste into github  
  9. ## Open page  at Githubs address for doing this  using default browser
  10.  
  11. clear
  12.  
  13.  
  14. # ANSI color codes
  15. BLUE='\033[0;34m'
  16. GREEN='\033[0;32m'
  17. RED='\033[0;31m'
  18. YELLOW='\033[0;33m'
  19. MAGENTA='\033[0;35m'
  20. CYAN='\033[0;36m'
  21. NC='\033[0m' # No Color
  22.  
  23. # Display header
  24. echo -e "${YELLOW}****************************************${NC}"
  25. echo -e "${BLUE}Creating SSH Keys by MD Harrington ${NC}"
  26. echo -e "${MAGENTA}$(date +"%Y-%m-%d %T")${NC}"
  27. echo -e "${YELLOW}****************************************${NC}"
  28.  
  29. echo ""
  30.  
  31.  
  32.  
  33. # Request email address and store in EMAIL variable
  34. read -p "Enter your email address: " EMAIL
  35.  
  36. # Check if email address has been entered
  37. if [[ -z $EMAIL ]]; then
  38.   echo -e "${RED}No email address entered.${NC} Exiting..."
  39.   exit 1
  40. fi
  41.  
  42. # Check if email address is valid
  43. if ! [[ "$EMAIL" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then
  44.   echo -e "${RED}Invalid email address entered.${NC} Exiting..."
  45.   exit 1
  46. fi
  47.  
  48.  
  49. # Display menu options
  50. while true
  51. do
  52.   echo -e "${YELLOW}***************${NC}"
  53.   echo -e "${YELLOW}*${NC} ${GREEN}Menu Options${NC} ${YELLOW}*${NC}"
  54.   echo -e "${YELLOW}***************${NC}"
  55.   echo -e "${RED}${CYAN}1${NC}${RED}. Create ssh key${NC}"
  56.   echo -e "${GREEN}${CYAN}2${NC}${GREEN}. Add your SSH private key to the ssh-agent${NC}"
  57.   echo -e "${YELLOW}${CYAN}3${NC}${YELLOW}. Copy ssh key to clipboard${NC}"
  58.   echo -e "${BLUE}${CYAN}4${NC}${BLUE}. Print ssh-key to terminal${NC}"
  59.   echo -e "${MAGENTA}${CYAN}5${NC}${MAGENTA}. Add key to Git-Hub${NC}"
  60.   echo -e "${CYAN}6${NC}. Exit menu"
  61.   echo ""
  62.  
  63.   # Get user choice
  64.   read -p "Enter your choice: " CHOICE
  65.  
  66.   # Perform action based on user choice
  67.   case $CHOICE in
  68.     1) # Create ssh key
  69.       ssh-keygen -t ed25519 -C $EMAIL
  70.       ;;
  71.     2) # Add your SSH private key to the ssh-agent
  72.       ssh-add ~/.ssh/id_ed25519
  73.       ;;
  74.     3) # Copy ssh key to clipboard
  75.    
  76.       # Check if pbcopy is installed and install if not
  77.     if ! command -v pbcopy &> /dev/null; then
  78.         clear
  79.         echo ""
  80.         echo ""
  81.         echo "*****************   NOTE IMPORTANT !! IMPORTANT   *****************************"
  82.         echo -e "${YELLOW}This script requires pbcopy${NC}"
  83.         echo -e "${RED}However pbcopy not found.${NC}"
  84.         echo -e "${BLUE}Please use option 4 of menu and copy key direct from terminal.${NC}"
  85.         echo "*******************************************************************************"
  86.         else
  87.             pbcopy < ~/.ssh/id_ed25519.pub
  88.             echo "SSH key copied to clipboard"
  89.     fi
  90.       ;;
  91.     4) # Print ssh-key to terminal
  92.       cat ~/.ssh/id_ed25519.pub
  93.       ;;
  94.     5) # Add key to Git-Hub
  95.       xdg-open https://github.com/settings/keys # opens webpage using default browser
  96.       ;;
  97.     6) # Exit menu
  98.       echo "Exiting menu..."
  99.       exit 0
  100.       ;;
  101.     *) # Invalid choice
  102.       echo -e "${RED}Invalid choice${NC}"
  103.       ;;
  104.   esac
  105.  
  106.  
  107.  
  108.  
  109. done
  110.  echo ""
  111.  echo "Hope this makes life far easier for you !"
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement