Skip to content

Instantly share code, notes, and snippets.

@trymzet
Created May 2, 2021 18:15
Show Gist options
  • Save trymzet/52e9b8e4da2f4e583d8b58d2c40ae006 to your computer and use it in GitHub Desktop.
Save trymzet/52e9b8e4da2f4e583d8b58d2c40ae006 to your computer and use it in GitHub Desktop.
A simple script for generating and uploading an SSH key to a server
# example usage: add_ssh_key -f dev -s domain.com
# stop on error
set -e
# defaults
filename=id_rsa
user=$USERNAME
# read args
while getopts f:s:u: flag
do
case "${flag}" in
f) filename=${OPTARG};;
s) server=${OPTARG};;
u) user=${OPTARG}
esac
done
extension=".pub"
full_filename=$filename$extension
cd ~/.ssh
ssh-keygen -f $filename -t rsa -b 4096 -q -N ""
cat $full_filename | ssh "$user@$server" "mkdir -p ~/.ssh; cat >> ~/.ssh/authorized_keys"
echo "Key uploaded successfully."
read
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment