Created
August 24, 2017 16:24
-
-
Save rjz/8b2913925c2c99bd0ceea090f7328c20 to your computer and use it in GitHub Desktop.
Add github public keys to authorized_keys
This file contains 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/bash | |
# | |
# Add a teammate's public key(s) to authorized_keys via the github API | |
# | |
# Usage: | |
# | |
# GITHUB_LOGIN=rjz ./add_github_keys.sh | |
# | |
# PREFIX for authorized_keys entries (default: attach to running | |
# tmux session) | |
# See: http://www.zeespencer.com/building-a-remote-pairing-setup/ | |
PREFIX="command=\"tmux attach\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding" | |
# Some reasonable assumptions (edit as needed) | |
SSHDIR="${HOME}/.ssh" | |
KEYFILE="${SSHDIR}/authorized_keys" | |
set -e | |
if [[ -z "$GITHUB_LOGIN" ]]; then | |
echo 'Please set $GITHUB_LOGIN' | |
exit 2 | |
fi | |
if [[ ! -d "$SSHDIR" ]]; then | |
mkdir -p $SSHDIR | |
fi | |
if [[ ! -f "$KEYFILE" ]]; then | |
touch "$KEYFILE" | |
fi | |
chmod 0600 "$KEYFILE" | |
add_key() { | |
local id=$1 | |
shift | |
local key=$(echo $@ | sed "s/'//g") | |
local comment="# $GITHUB_LOGIN: $id" | |
grep "^$comment" "$KEYFILE" > /dev/null || { | |
echo >> "$KEYFILE" | |
echo "$comment" >> "$KEYFILE" | |
echo "$PREFIX $key" >> "$KEYFILE" | |
} | |
} | |
curl -s https://api.github.com/users/$GITHUB_LOGIN/keys \ | |
| jq -r '.[] | [.id, .key] | @sh' | while read args; do | |
add_key $args | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment