Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to identify with a username and password to github.com servers for the purpose of adding an ssh key to the github user account? So far everything I've read suggests that a user's ssh key must be added via the web GUI. I'm looking for the method or process of adding a key via a command line interface or else a bash/ansible/something script.

share|improve this question
1  
How about using some library to use GitHub's API? – sr_ Jun 13 '14 at 7:08
up vote 9 down vote accepted

Auth with username and password is supported by github api:

There are three ways to authenticate through GitHub API v3. ...
Basic Authentication
$ curl -u "username" https://api.github.com
...

So just choose a lib in the language you prefer and use the implemented version of the Create a Public Key "Public Key" API Section:

Creates a public key. Requires that you are authenticated via Basic Auth, or OAuth with at least [write:public_key] scope.

INPUT
POST /user/keys

{
    "title": "octocat@octomac",
    "key": "ssh-rsa AAA..."
}

If you want to use it from command line (via curl):

curl -u "username" --data '{"title":"test-key","key":"ssh-rsa AAA..."}' https://api.github.com/user/keys

or even without prompting for password:

curl -u "username:password" --data '{"title":"test-key","key":"ssh-rsa AAA..."}' https://api.github.com/user/keys

here is a nice little tutorial for using curl to interact with github API

share|improve this answer
    
This is exactly the kind of information I was looking for! Thank you very much! – cmosetick Jun 13 '14 at 20:01

Similar to xx4h's answer, this is how I do it in scripts for automating new VM setups.

ssh-keygen -t rsa -b 4096 -C "[email protected]"
curl -u "myusername" \
    --data "{\"title\":\"DevVm_`date +%Y%m%d%H%M%S`\",\"key\":\"`cat ~/.ssh/id_rsa.pub`\"}" \
    https://api.github.com/user/keys

It gives you a new SSH key, includes it in the curl call and gives a unique but still easily identifiable name for each one on the GitHub side (e.g. running now would give DevVm_150602142247).

share|improve this answer

Another option is to use an API token... I use the following on our internal gitLab server

snippet:

#!/bin/bash

myemail="[email protected]"

# User API token can be found: "https://git.labs.domain.com/profile/account"
git_api_token="m3iP27Jh8KSgNmWAksYp"

# We'll use the HTTPS to push a ssh key to git, SSH for pull/push configuration
gitrepo_ssh="[email protected]:devops/automation.git"
gitrepo_https="https://git.labs.domain.com/devops/automation.git"

########################]  D O   N O T   C H A N G E  [########################

# Generating SSH key:
ssh-keygen -f "${HOME}/.ssh/id_rsa" -t rsa -b 4096 -C "${myemail}" -N ''
sslpub="$(cat ${HOME}/.ssh/id_rsa.pub |tail -1)"

# git API path for posting a new ssh-key:
git_api_addkey="https://$(echo ${gitrepo_https} |cut -d'/' -f3)/api/v3/user/keys"

# lets name the ssh-key in get after the hostname with a timestamp:
git_ssl_keyname="$(hostname)-$(date +%Y%m%d%H%M%S)"

# Finally lets post this ssh key:
curl -H "PRIVATE-TOKEN: ${git_api_token}" -H "Content-Type: application/json" \
    -X POST -d "{\"title\":\"${git_ssl_keyname}\",\"key\":\"${sslpub}\"}"     \
    ${git_api_addkey}
share|improve this answer
#!/bin/bash

set -xe
myemail="your-email"

#your personal access token
git_api_token="befdf14c152d6f2ad8cff9c5affffffffffffffffff"

#We'll use the HTTPS to push a ssh key to git, SSH for pull/push configuration
gitrepo_ssh="[email protected]:person/repo.git"
gitrepo_https="https://github.com/person/repo.git"

#Generating SSH key:
ssh-keygen -f "${HOME}/.ssh/id_rsa" -t rsa -b 4096 -C "${myemail}" -N ''
sslpub="$(cat ${HOME}/.ssh/id_rsa.pub |tail -1)"

#git API path for posting a new ssh-key:
git_api_addkey="https://api.$(echo ${gitrepo_https} |cut -d'/' -f3)/user/keys"

#lets name the ssh-key in get after the hostname with a timestamp:
git_ssl_keyname="$(hostname)_$(date +%d-%m-%Y)"

#Finally lets post this ssh key:
curl -H "Authorization: token ${git_api_token}" -H "Content-Type: application/json" -X POST -d "{\"title\":\"${git_ssl_keyname}\",\"key\":\"${sslpub}\"}" ${git_api_addkey}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.