Skip to content

Instantly share code, notes, and snippets.

@rtlong
Last active February 13, 2017 15:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rtlong/6790049 to your computer and use it in GitHub Desktop.
Save rtlong/6790049 to your computer and use it in GitHub Desktop.
Put public keys for a github user in ~/.ssh/authorized keys with mucho ease
IFS="$(printf '\n\t')"
mkdir -p ~/.ssh
if ! [[ -f ~/.ssh/authorized_keys ]]; then
echo "Creating new ~/.ssh/authorized_keys"
touch ~/.ssh/authorized_keys
fi
user=$1
keys=`curl https://api.github.com/users/$user/keys | grep -o -E "ssh-\w+\s+[^\"]+"`
for key in $keys; do
echo $key
grep -q "$key" ~/.ssh/authorized_keys || echo "$key" >> ~/.ssh/authorized_keys
done
@rtlong
Copy link
Author

rtlong commented Oct 2, 2013

Use this like so:

$ curl -L http://bit.ly/gh-keys | bash -s <github username>

http://bit.ly/gh-keys redirects to the raw version of this script; curl's -L tells it to follow redirects, and bash's -s tells it to use STDIN as the script and the arguments as the positional parameters

@musically-ut
Copy link

At the time of writing, https://github.com/musically-ut.keys will output the keys in authorized_keys friendly format and in plain text. That will avoid having to grep through the JSON looking for the keys.

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