├── ssh-keyreg.zsh ├── src └── _ssh-keyreg ├── README.md └── bin └── ssh-keyreg /ssh-keyreg.zsh: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | 3 | # zsh script 4 | if [[ -n $ZSH_VERSION ]]; then 5 | # Add to the PATH 6 | export PATH=$PATH:${0:A:h}/bin 7 | fpath=(${0:A:h}/src(N-/) $fpath) 8 | fi 9 | -------------------------------------------------------------------------------- /src/_ssh-keyreg: -------------------------------------------------------------------------------- 1 | #compdef ssh-keyreg 2 | 3 | local curcontext="$curcontext" state line 4 | local -A opt_args 5 | 6 | _arguments -C \ 7 | '(- :)'{-h,--help}'[Show this help and exit]' \ 8 | '(-p --path)'{-p,--path}'[Specify the path of public key]:public keys:_files -W ~/.ssh -g "*.pub"' \ 9 | '(-u --user)'{-u,--user}'[Specify username]:username:(${$(git config --get user.name)} $USER)' \ 10 | '(-d --desc)'{-d,--desc}'[Set desctiption to the registration]' \ 11 | '(-)*: :(github bitbucket)' && ret=0 12 | 13 | return ret 14 | 15 | # vim: ft=zsh sw=2 ts=2 et 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ssh-keyreg 2 | === 3 | 4 | [![](http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)][license] 5 | 6 | ssh-keyreg is command-line method or programmatically add ssh key to github.com user account. 7 | 8 | ## Description 9 | 10 | >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. 11 | > via [command line method or programmatically add ssh key to github.com user account](http://unix.stackexchange.com/questions/136894/command-line-method-or-programmatically-add-ssh-key-to-github-com-user-account) 12 | 13 | ***DEMO:*** 14 | 15 | ![DEMO](https://raw.githubusercontent.com/b4b4r07/screenshots/master/ssh-keyreg/demo.gif) 16 | 17 | ## Features 18 | 19 | - command-line method add ssh key to github.com 20 | - support bash/zsh 21 | 22 | ## Usage 23 | 24 | ```console 25 | $ ssh-keyreg --help 26 | usage: ssh-keyreg [-h|--help][[-d|--desc ][-u|--user ][-p|--path ]] [github|bitbucket] 27 | command line method or programmatically add ssh key to github.com user account 28 | 29 | options: 30 | -h, --help show this help message and exit 31 | -d, --desc description of registration 32 | -u, --user username and password (user:pass) 33 | -p, --path path of public key 34 | 35 | MIT @b4b4r07 36 | ``` 37 | 38 | For example, this is a basical usage below. 39 | 40 | ```console 41 | $ cd ~/.ssh; ssh-keygen 42 | $ ssh-keyreg --path id_rsa.rub github 43 | ``` 44 | 45 | ## Installation 46 | 47 | ### Using [zplug](https://github.com/b4b4r07/zplug) for zsh user 48 | 49 | ```console 50 | $ zplug "b4b4r07/ssh-keyreg", as:command, use:bin 51 | $ zplug install 52 | ``` 53 | 54 | ### zplug-free install 55 | 56 | To install this tool without zplug: 57 | 58 | ```console 59 | $ sudo sh -c "curl https://raw.githubusercontent.com/b4b4r07/ssh-keyreg/master/bin/ssh-keyreg -o /usr/local/bin/ssh-keyreg && chmod +x /usr/local/bin/ssh-keyreg" 60 | ``` 61 | 62 | ssh-keyreg is a shell script, so put it somewhere and make sure it's added to your `$PATH`. 63 | 64 | ## License 65 | 66 | [MIT][license] © BABAROT (a.k.a. [b4b4r07](http://b4b4r07.com)) 67 | 68 | [license]: http://b4b4r07.mit-license.org 69 | -------------------------------------------------------------------------------- /bin/ssh-keyreg: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Author: BABAROT (@b4b4r07) 3 | # License: MIT License 4 | 5 | # references 6 | # * {http://unix.stackexchange.com/questions/136894/ 7 | # command-line-method-or-programmatically- 8 | # add-ssh-key-to-github-com-user-account 9 | # } 10 | # * {https://github.com/ABCanG/add-sshkey-remote} 11 | 12 | die() { 13 | echo "$@" >&2 14 | exit 1 15 | } 16 | 17 | usage() { 18 | cat <][-u|--user ][-p|--path ]] [github|bitbucket] 20 | command line method or programmatically add ssh key to github.com user account 21 | 22 | options: 23 | -h, --help show this help message and exit 24 | -d, --desc description of registration 25 | -u, --user username and password (user:pass) 26 | -p, --path path of public key 27 | 28 | MIT @b4b4r07 29 | HELP 30 | } 31 | 32 | while (($# > 0)); do 33 | 34 | # check flags and arguments 35 | case "$1" in 36 | -h|--help) 37 | die "$(usage)" 38 | ;; 39 | 40 | -d|--desc) 41 | [[ -z $1 ]] && die "$1: require arguments" 42 | desc="$2"; shift; 43 | ;; 44 | 45 | -u|--user) 46 | [[ -z $1 ]] && die "$1: require arguments" 47 | user="$2"; shift; 48 | ;; 49 | 50 | -p|--path) 51 | [[ -z $1 ]] && die "$1: require arguments" 52 | path=~/.ssh/"${2##*/}"; shift 53 | ;; 54 | 55 | github) 56 | servies="github" 57 | ;; 58 | 59 | bitbucket) 60 | servies="bitbucket" 61 | ;; 62 | esac 63 | shift 64 | done 65 | 66 | desc="${desc:-$(date +%D)}" 67 | user="${user:-$(git config --get user.name)}" 68 | path="${path:-$HOME/.ssh/id_rsa.pub}" 69 | title="$(hostname) ($desc)" 70 | 71 | # check if the path is available 72 | [[ -f $path ]] || die "$path: no such file or directory" 73 | key_data="$(cat "$path")" 74 | 75 | case "$servies" in 76 | ""|github) 77 | result="$( 78 | curl -u "${user:=$USER}" \ 79 | --data "{\"title\":\"$title\",\"key\":\"$key_data\"}" \ 80 | https://api.github.com/user/keys 81 | )" 82 | ;; 83 | 84 | bitbucket) 85 | result="$( 86 | curl -u "${user:=$USER}" \ 87 | --data "label=$label" \ 88 | --data-urlencode "key=$key_data" \ 89 | https://bitbucket.org/api/1.0/users/"${user%:*}"/ssh-keys 90 | )" 91 | ;; 92 | esac 93 | 94 | # check if upload is completed successfully 95 | ssh -T git@github.com 2>&1 | grep "success" 96 | if [[ $? -ne 0 ]]; then 97 | die "sorry, try again" 98 | fi 99 | --------------------------------------------------------------------------------