Skip to content

Instantly share code, notes, and snippets.

@lalyos
Last active December 25, 2015 19:29
Show Gist options
  • Save lalyos/7027890 to your computer and use it in GitHub Desktop.
Save lalyos/7027890 to your computer and use it in GitHub Desktop.
git clone https://gist.github.com/7027890.git && 7027890/gh-upload-ssh-key.sh

or in case you don't have git installed on the machine (why would you then use this scipt?), anyway there is an url easier to remember 'http://j.mp/github-key'

curl -sLo github-key.sh http://j.mp/github-key && . github-key.sh
#!/bin/bash
################################################################################
# To use this script copy and paste the following line without the "#" character
# into a terminal on linux/mac or into 'gitbash' on Windows
# gitbash is part of: MySysgit http://msysgit.github.io/
################################################################################
# curl -sLo github-key.sh http://j.mp/github-key && ./github-key.sh
################################################################################
GITHUB_TOKEN=$1
KEY_FILE=~/.ssh/id_rsa.pub
PRIVATE_KEY_FILE=${KEY_FILE%.*}
GITHUB_KEYNAME=my-private-key-uploaded-by-script
########################################
# asks for a githug personal token
# with user scope
########################################
if [[ "$GITHUB_TOKEN" == "" ]] ; then
read -p "create a new github personal token at https://github.com/settings/tokens/new with Token description: 'ssh-key-upload-script' then click 'CREATE TOKEN' once it ready, copy paste the 40 character long token here: " GITHUB_TOKEN
fi
########################################
# checks key length
########################################
if [ ${#GITHUB_TOKEN} -ne 40 ] ; then
echo "[ERROR] the github token should be 40 chars long."
exit -1;
fi
########################################
# checks public key, generates a default
# without password
########################################
if [ -f $KEY_FILE ] ; then
echo "your existing public key will be used: $KEY_FILE"
echo " fingerprint of public key:"
ssh-keygen -lf $KEY_FILE
else
echo "public/private ssh keypair will be generated ..."
ssh-keygen -q -t rsa -f $PRIVATE_KEY_FILE -N ""
fi
########################################
# adds new key via github API
########################################
curl $OPTS -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/user/keys -d@- <<EOF
{
"title" : "$GITHUB_KEYNAME",
"key" : "$(cat $KEY_FILE)"
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment