Last active
March 3, 2016 05:41
-
-
Save ernoaapa/800a84934f7d909b3a4b to your computer and use it in GitHub Desktop.
Prints all GitHub organization users SSH keys. Can be used in automation to add github users to ~/.ssh/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 | |
# | |
# Fetches all GitHub organization users SSH keys and print out | |
# Can be used to update ~/.ssh/authorized_keys file | |
# | |
# Eg. | |
# fetch-github-organization-members-ssh-keys.sh -o my-org -t TOKEN > ~/.ssh/authorized_keys | |
# | |
function parse_arguments() { | |
while [[ $# > 1 ]]; do | |
key="$1" | |
case $key in | |
-o|--organization) | |
ORGANIZATION="$2" | |
shift | |
;; | |
-t|--token) | |
GITHUB_TOKEN="$2" | |
shift | |
;; | |
*) | |
echo "Unknown argument: $key"; | |
exit 2; | |
;; | |
esac | |
shift | |
done | |
if [ "${ORGANIZATION}" == "" ]; then | |
echo "--organization is required parameter" | |
exit 1 | |
fi | |
if [ "${GITHUB_TOKEN}" == "" ]; then | |
echo "--token is required parameter" | |
exit 1 | |
fi | |
} | |
function main() { | |
members=`get_organization_members ${ORGANIZATION} ${GITHUB_TOKEN}` | |
for member in ${members}; do | |
member_ssh_keys=`curl -s https://github.com/${member}.keys` | |
if [ "${member_ssh_keys}" != "" ]; then | |
# Append user name at the end of key | |
# Note: GitHub strip off the username@computername from the key | |
echo "${member_ssh_keys} ${member}@GitHub" | |
fi | |
done | |
} | |
function get_organization_members() { | |
organization=$1 | |
token=$2 | |
# Fetch users and parse username from the response | |
curl -s -H "Authorization: token ${token}" https://api.github.com/orgs/${organization}/members | sed -En 's/(.*login\": \"(.*)\".*)/\2/p' | |
} | |
parse_arguments $@ | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment