Skip to content

Instantly share code, notes, and snippets.

@kaganjd kaganjd/get_keys.py
Created Jul 10, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.