Last active
December 16, 2015 04:39
-
-
Save shiraji/5378383 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/sh | |
#get GitHub user name if it is not provided | |
if [ $# -lt 1 ]; then | |
echo -n "Enter GitHub user name: " | |
read _githubUsername | |
else | |
_githubUsername=$1 | |
fi | |
#get GitHub password | |
echo -n "Enter GitHub password: " | |
read -s _githubPassword | |
echo | |
#get SSH password | |
echo -n "Enter SSH Key password: " | |
read -s _sshKeyPassword | |
echo | |
#get GitHub SSH title | |
echo "Enter GitHub SSH Title [hostname -s]: " | |
read _githubSSHKeyTitle | |
_githubSSHKeyTitle=${_githubSSHKeyTitle:`hostname -s`} | |
#github settings | |
_githubAuthURL="https://api.github.com/authorizations" | |
_githubUserKeyURL="https://api.github.com/user/keys" | |
#ssh key settings | |
_sshKeyDir="$HOME/.ssh" | |
_sshKeyFile="$_sshKeyDir/id_rsa" | |
_sshKeyPubFile=$_sshKeyFile".pub" | |
#create ssh folder if it does not exist | |
if [ ! -e $_sshKeyDir ]; then | |
mkdir -p $_sshKeyDir | |
fi | |
#run ssh-keygen if private key file does not exist | |
if [ ! -e $_sshKeyFile ]; then | |
ssh-keygen -f $_sshKeyFile -N "$_sshKeyPassword" | |
fi | |
#generate a token | |
_token=`curl -u "${_githubUsername}:${_githubPassword}" -d "{\"scopes\":[\"user\"]}" $_githubAuthURL | grep "token" | cut -d"\"" -f4` | |
#post ssh public key | |
curl -H "Authorization: token $_token" $_githubUserKeyURL -d "{ \"title\":\"$_githubSSHKeyTitle\", \"key\":\"`cat $_sshKeyPubFile`\" }" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment