Skip to content

Instantly share code, notes, and snippets.

@kaganjd
Created July 10, 2019 21:35
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 kaganjd/1508c58d123edca5c2154c66c7b09b93 to your computer and use it in GitHub Desktop.
Save kaganjd/1508c58d123edca5c2154c66c7b09b93 to your computer and use it in GitHub Desktop.
Python script to get a GitHub user's public SSH keys and write to .ssh/authorized_keys
# Run with `python3 get_keys.py [user]`
import requests
import subprocess
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("user")
args = parser.parse_args()
DIR_PATH = '.ssh'
FILENAME = 'authorized_keys'
MKDIR_SSH = 'mkdir {}'.format(DIR_PATH)
# chmod 700: R, W, X for file owner; no access for others.
SET_PERMISSIONS_SSH_DIR = 'chmod 700 {}'.format(DIR_PATH)
TOUCH_KEYFILE = 'touch {}/{}'.format(DIR_PATH, FILENAME)
# chmod 644: R, W for file owner; R-only for others.
SET_PERMISSIONS_KEYFILE = 'chmod 644 {}/{}'.format(DIR_PATH, FILENAME)
def run_split_command(command):
split_command = command.split()
subprocess.run(split_command)
run_split_command(MKDIR_SSH)
run_split_command(SET_PERMISSIONS_SSH_DIR)
run_split_command(TOUCH_KEYFILE)
run_split_command(SET_PERMISSIONS_KEYFILE)
keys = requests.get('https://github.com/{}.keys'.format(args.user))
with open('{}/{}'.format(DIR_PATH, FILENAME), 'w') as key_file:
key_file.write(keys.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment