Checks ~/.ssh/authorized_keys
against a list of Github users and checks who has access.
Usage:
$ python github-ssh-check.py -u nlindblad
Example output:
nlindblad - 2 keys present
import argparse | |
import os | |
import sys | |
import urllib2 | |
def parse_arguments(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-u','--users', nargs='+', help='Github user(s)', required=True) | |
args = parser.parse_args() | |
return args | |
def keys_from_string(lines): | |
return set([key.split()[1] for key in lines.split('\n') if len(key) > 0]) | |
def github_keys_for_username(username): | |
response = urllib2.urlopen('https://github.com/%s.keys' % username) | |
return keys_from_string(response.read()) | |
def check_keys_present(all_user_keys, authorized_keys): | |
for user, user_keys in all_user_keys.iteritems(): | |
nbr_of_keys_present = len(authorized_keys.intersection(user_keys)) | |
print "%s - %d keys present" % (user, nbr_of_keys_present) | |
def main(authorized_keys_file): | |
args = parse_arguments() | |
try: | |
with open(authorized_keys_file, 'r') as f: | |
authorized_keys = keys_from_string(f.read()) | |
except Exception as e: | |
print "Could not read %s: %s" % (authorized_keys_file, e) | |
sys.exit(1) | |
user_keys = {} | |
for user in args.users: | |
try: | |
user_keys[user] = github_keys_for_username(user) | |
except Exception as e: | |
print "Could not get Github SSH keys for %s: %s" % (user, e) | |
check_keys_present(user_keys, authorized_keys) | |
if __name__ == '__main__': | |
main(os.path.join(os.environ['HOME'], '.ssh/authorized_keys')) |