Created
May 2, 2021 18:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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