Skip to content

Instantly share code, notes, and snippets.

@thewellington
Last active December 24, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thewellington/6736208 to your computer and use it in GitHub Desktop.
Save thewellington/6736208 to your computer and use it in GitHub Desktop.
Used to upload keys to specific accounts usage: upload-authorized-keys.sh shellacct@hostname /path/to/key.pub
#!/bin/sh
#
# upload-authorized-keys.sh
# by bill@wellingtonnet.net
#
# This script will take a key provided as an argument on the commandline and
# add it to the authorized_keys file on a remote server. It can be used to
# upload many keys in a short time, particularly once your key is already loaded.
#
# Usage: upload-authorized-keys.sh shellacct@hostname /path/to/local/copy/of/key.pub
#
# If no second arg, use public key in ~/.ssh
if [ -z $2 ]
then
# KEY="$HOME/.ssh/id_dsa.pub"
KEY="$HOME/.ssh/id_rsa.pub"
if [ ! -f ~/.ssh/id_rsa.pub ];then
echo "private key not found at $KEY"
echo "* please create it with "ssh-keygen -t [rsa|dsa]" *"
echo "* to login to the remote host without a password, don't give the key you create with ssh-keygen a password! *"
exit
fi
else
KEY=$2
fi
# If no first arg, then no destination! complain!
if [ -z $1 ]
then
echo "Please specify user@host.tld as the first switch to this script"
exit
fi
echo "Adding $KEY to $1"
KEYCODE=`cat $KEY`
ssh -q $1 "mkdir ~/.ssh 2>/dev/null; chmod 700 ~/.ssh; echo "$KEYCODE" >> ~/.ssh/authorized_keys; chmod 644 ~/.ssh/authorized_keys"
echo "done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment