Created
May 16, 2021 19:35
-
-
Save patrikalienus/49c4b90f77be03d184607f17dc74e5d4 to your computer and use it in GitHub Desktop.
SSH Key upload script for Bash
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
#!/usr/bin/env bash | |
# Upload SSH keys to server | |
function upload_ssh_keys () { | |
if [ "$1" != "" ] | |
then | |
host=$1 | |
port=`echo -n $host | cut -d: -f 2` | |
host=`echo -n $host | cut -d: -f 1` | |
echo "Using $host as host" | |
echo "Using $port as port" | |
port_text="" | |
if [ "$port" != "$host" ] | |
then | |
port_text=" -p $port" | |
fi | |
cat ~/.ssh/id_rsa.pub | ssh $port_text $host "mkdir -p .ssh; touch .ssh/authorized_keys; cat - >> .ssh/authorized_keys; chmod 700 .ssh; chmod 600 .ssh/authorized_keys" | |
echo "Key(s) uploaded to $host $port_text" | |
else | |
echo "'$host' is no valid host. Try upload_ssh_keys user@hostname or upload_ssh_keys user@hostname:port" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment