Skip to content

Instantly share code, notes, and snippets.

@godber
Forked from mattwillsher/grant_github_user_access
Last active May 6, 2023 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godber/8835a879cc916c9b3e723c50cab1eb1b to your computer and use it in GitHub Desktop.
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.
#!/bin/bash
#
# (c)2014 Matt Willsher <matt@willsherpartners.co.uk>
#
# 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
#!/bin/bash
#
# (c)2014 Matt Willsher <matt@willsherpartners.co.uk>
#
# 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
#!/bin/bash
#
# (c)2014 Matt Willsher <matt@willsherpartners.co.uk>
#
# 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
@godber
Copy link
Author

godber commented May 4, 2023

This should probably ensure that ~/.ssh is properly created

mkdir ~/.ssh
chmod 700 ~/.ssh

@godber
Copy link
Author

godber commented May 4, 2023

Or better yet just run mkdir -m 700 -p ~/.ssh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment