|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Exit immediately if a command exits with a non-zero status |
| 4 | +set -e |
| 5 | + |
| 6 | +# Function to check if a command is available |
| 7 | +command_exists() { |
| 8 | + command -v "$1" >/dev/null 2>&1 |
| 9 | +} |
| 10 | + |
| 11 | +# Check if ssh-keygen is available |
| 12 | +if ! command_exists ssh-keygen; then |
| 13 | + echo "ssh-keygen is not installed. Installing..." |
| 14 | + sudo apt-get update |
| 15 | + sudo apt-get install -y openssh-client |
| 16 | +fi |
| 17 | + |
| 18 | +# Check if ssh-agent is running |
| 19 | +if ! pgrep -u "$USER" ssh-agent >/dev/null; then |
| 20 | + echo "Starting ssh-agent..." |
| 21 | + eval "$(ssh-agent -s)" |
| 22 | +fi |
| 23 | + |
| 24 | +# Function to generate SSH key for an account |
| 25 | +generate_ssh_key() { |
| 26 | + local email=$1 |
| 27 | + local key_name=$2 |
| 28 | + local key_file="$HOME/.ssh/id_rsa_$key_name" |
| 29 | + |
| 30 | + echo "Generating SSH key for $email..." |
| 31 | + ssh-keygen -t rsa -b 4096 -C "$email" -f "$key_file" -N "" |
| 32 | + |
| 33 | + # Set permissions for private and public keys |
| 34 | + chmod 600 "$key_file" |
| 35 | + chmod 644 "${key_file}.pub" |
| 36 | + |
| 37 | + ssh-add "$key_file" |
| 38 | + echo "SSH key generated and added to the agent for $email." |
| 39 | +} |
| 40 | + |
| 41 | +# Add SSH keys for multiple accounts |
| 42 | +while true; do |
| 43 | + read -p "Enter your GitHub email address (or press Enter to finish): " email |
| 44 | + if [ -z "$email" ]; then |
| 45 | + break |
| 46 | + fi |
| 47 | + |
| 48 | + # Extract the part before the @ symbol |
| 49 | + key_name=$(echo "$email" | cut -d '@' -f 1) |
| 50 | + generate_ssh_key "$email" "$key_name" |
| 51 | + |
| 52 | + echo "Your SSH public key for $email is:" |
| 53 | + cat "$HOME/.ssh/id_rsa_$key_name.pub" |
| 54 | + |
| 55 | + read -p "Open GitHub SSH keys settings page in your default browser? (y/n): " open_browser |
| 56 | + if [[ $open_browser == "y" || $open_browser == "Y" ]]; then |
| 57 | + xdg-open "https://github.com/settings/keys" |
| 58 | + fi |
| 59 | + |
| 60 | + echo "Please add the above SSH public key to your GitHub account." |
| 61 | +done |
| 62 | + |
| 63 | +# Create or update SSH config file for multiple accounts |
| 64 | +echo "Creating/updating SSH config file..." |
| 65 | +ssh_config="$HOME/.ssh/config" |
| 66 | +touch "$ssh_config" |
| 67 | +chmod 600 "$ssh_config" |
| 68 | + |
| 69 | +for key_file in ~/.ssh/id_rsa_*; do |
| 70 | + if [[ -f "$key_file" && "$key_file" != ~/.ssh/id_rsa ]]; then |
| 71 | + key_name=$(basename "$key_file" | sed 's/id_rsa_//') |
| 72 | + echo "Host github-$key_name" >> "$ssh_config" |
| 73 | + echo " HostName github.com" >> "$ssh_config" |
| 74 | + echo " User git" >> "$ssh_config" |
| 75 | + echo " IdentityFile $key_file" >> "$ssh_config" |
| 76 | + echo "" >> "$ssh_config" |
| 77 | + fi |
| 78 | +done |
| 79 | + |
| 80 | +echo "SSH config file created/updated." |
| 81 | + |
| 82 | +echo "SSH key setup script completed." |
0 commit comments