命令行方法或以编程方式将ssh密钥添加到github.com用户帐户


18

有没有一种方法可以使用github.com服务器上的用户名和密码进行标识,以向github用户帐户添加ssh密钥?到目前为止,我所读的所有内容都建议必须通过Web GUI添加用户的ssh密钥。我正在寻找通过命令行界面或bash / ansible / something脚本添加密钥的方法或过程。


1
使用某些库使用GitHub的API怎么样?
sr_

Answers:


15

github api支持使用用户名和密码进行身份验证:

有三种通过GitHub API v3进行身份验证的方法。...
基本身份验证
$卷曲-U “用户名” https://api.github.com
...

因此,只需您喜欢的语言选择一个库,然后使用“ 创建公钥 ”“公钥” API部分的实现版本:

创建一个公共密钥。要求您通过至少具有[write:public_key]范围的Basic Auth或OAuth进行身份验证。

输入
POST /user/keys

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

如果要从命令行使用它(通过curl):

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

甚至不提示输入密码:

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

这是一个使用curl与github API交互的不错的小教程


这正是我一直在寻找的信息!非常感谢你!
cmosetick

7

与xx4h的答案类似,这就是我在脚本中自动执行新VM设置的方式。

ssh-keygen -t rsa -b 4096 -C "myemailaddress@hotmail.com"
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

它为您提供了一个新的SSH密钥,将其包含在curl调用中,并为GitHub端的每个密钥提供了一个唯一但仍易于识别的名称(例如,现在运行将提供DevVm_150602142247)。


1
#!/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="git@github.com: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}

0

另一个选择是使用API​​令牌...我在内部gitLab服务器上使用以下内容

片段:

#!/bin/bash

myemail="first.last@domain.com"

# 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="git@git.labs.domain.com: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}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.