Created
July 10, 2019 21:35
-
-
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
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
# 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