-
-
Save godber/8835a879cc916c9b3e723c50cab1eb1b to your computer and use it in GitHub Desktop.
A script to pull SSH keys for a give GitHub user and add those keys to the current users authorized_keys file.
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 | |
# | |
# (c)2014 Matt Willsher <[email protected]> | |
# | |
# Licensed under GPLv3 http://www.gnu.org/licenses/gpl.txt | |
# | |
umask 077 | |
if [[ $EUID == 0 ]]; then | |
echo "This script can't be used as root" >&2 | |
exit 1 | |
fi | |
tmpfile="/tmp/authkeys.$$" | |
rc=0 | |
if [[ $# -ne 1 ]]; then | |
echo "Please give a github user account as a parameter" >&2 | |
exit 1 | |
fi | |
mkdir -m 700 -p ~/.ssh | |
githubuser=$1 | |
fullname=$( curl -s https://api.github.com/users/${githubuser} | jq -r '.name' ) | |
keycomment="$fullname github:${githubuser}" | |
curl -s https://github.com/${githubuser}.keys >$tmpfile | |
if [[ $( stat -c'%s' $tmpfile ) -gt 1 ]]; then | |
sed -i -e "s/$/ ${keycomment}/" $tmpfile | |
cat $tmpfile >>~/.ssh/authorized_keys | |
echo >>~/.ssh/authorized_keys | |
else | |
echo "Couldn't get any keys. Does the github account exist?" >&2 | |
rc=1 | |
fi | |
rm $tmpfile | |
echo "Added keys for $githubuser, full name $fullname" | |
exit $rc |
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 | |
# | |
# (c)2014 Matt Willsher <[email protected]> | |
# | |
# Licensed under GPLv3 http://www.gnu.org/licenses/gpl.txt | |
# | |
umask 077 | |
if [[ $EUID == 0 ]]; then | |
echo "This script can't be used as root" >&2 | |
exit 1 | |
fi | |
cut -d' ' -f1,2 --complement ~/.ssh/authorized_keys | sort | uniq -c | |
exit $rc |
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 | |
# | |
# (c)2014 Matt Willsher <[email protected]> | |
# | |
# Licensed under GPLv3 http://www.gnu.org/licenses/gpl.txt | |
# | |
umask 077 | |
if [[ $EUID == 0 ]]; then | |
echo "This script can't be used as root" >&2 | |
exit 1 | |
fi | |
if [[ $# -ne 1 ]]; then | |
echo "Please give a github user account as a parameter" >&2 | |
exit 1 | |
fi | |
githubuser=$1 | |
fullname=$( curl -s https://api.github.com/users/${githubuser} | jq -r '.name' ) | |
keycomment="$fullname github:${githubuser}" | |
grep "${keycomment}" ~/.ssh/authorized_keys >/dev/null | |
if [[ $? -ne 0 ]]; then | |
echo "User $githubuser doesn't have keys in the keys file" >&2 | |
exit 1 | |
fi | |
sed -i -e "/${keycomment}/d" ~/.ssh/authorized_keys | |
rc=$? | |
if [[ $rc == 0 ]]; then | |
echo "Removed keys for $githubuser, full name $fullname" | |
else | |
echo "An error occured removing keys from the authorized_keys file." >&2 | |
fi | |
exit $rc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or better yet just run
mkdir -m 700 -p ~/.ssh