Skip to content

Instantly share code, notes, and snippets.

@sankita15
Created December 11, 2022 22:05
Show Gist options
  • Save sankita15/875c60ecc028d471f75f102c70b62681 to your computer and use it in GitHub Desktop.
Save sankita15/875c60ecc028d471f75f102c70b62681 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Ask the user for their key type
echo Which type of key you want to generate? Please select from [rsa, ed25519]
read keyType
echo Please enter your email
read email
# Ask user with which they want to tag the new key
echo Please provide a hostname which you want to use for this key
read hostname
echo Please provide your github username
read username
echo Please provide your repository name
read repositoryname
if [ $keyType = "rsa" ]
then
eval "ssh-keygen -t rsa -b 4096 -C $email"
elif [ $keyType = "ed25519" ]
then
eval "ssh-keygen -t ed25519 -C $email"
else
echo "Please enter system type from the above list"
return
fi
echo "------------------------------------------------------"
echo "Your ssh key got generated in your home .ssh directory"
echo "------------------------------------------------------\n"
echo "Adding you ssh key to ssh-agent...."
eval "$(ssh-agent -s)"
echo "\n"
if [ -f ~/.ssh/config ]
then
echo "Config file found.\n"
echo "Adding github host to existing ssh config file..."
echo "\n\n\nHost $hostname\nHostname github.com\nAddKeysToAgent yes\nUseKeychain yes\nIdentityFile ~/.ssh/id_$keyType\nIdentitiesOnly yes" >> ~/.ssh/config
else
echo "\nCreating a new ssh config file..."
eval "touch ~/.ssh/config"
echo "\n"
echo "Adding github host to new ssh config file..."
echo "Host *.github.com\nAddKeysToAgent yes\nUseKeychain yes\nIdentityFile ~/.ssh/id_$keyType" >> ~/.ssh/config
fi
echo "Adding new ssh key to ssh agent...\n"
eval "ssh-add -K ~/.ssh/id_$keyType"
echo "Key got added!!! Now copy the public key from below and add to your github.\n\n"
echo "Your new ssh public key:"
eval "cat ~/.ssh/id_$keyType.pub"
echo "\n Now update your project .git/config file url key with below url:"
echo "git@$hostname:$username/$repositoryname.git"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment