10
# suitable for use in cloud-config
12
# suitable for use in cloud-localds meta-data
14
# simple json list of strings keys
20
return json.dumps(data, indent=2, sort_keys=True, separators=(',', ': '))
26
out = subprocess.check_output(['ssh-add', '-L'],
27
stderr=subprocess.STDOUT)
28
if isinstance(out, bytes):
30
keys += out.splitlines()
34
ssh_dir = os.path.expanduser('~' + os.path.sep + '.ssh')
35
keyfiles = ['id_ecdsa.pub', 'id_ed25519.pub', 'id_rsa.pub']
36
for f in [os.path.join(ssh_dir, f) for f in keyfiles]:
38
with open(f, "r") as fp:
39
keys += fp.read().splitlines()
41
return list(set(keys))
44
def format_pubkeys(mode, keys):
45
if mode not in VALID_MODES:
46
raise ValueError("unknown mode '%s'. Expected one of: %s" %
47
(mode, ' '.join(VALID_MODES)))
50
data = '\n'.join(keys) + "\n"
51
elif mode == "cloud-config" or mode == "meta-data":
53
if mode == "cloud-config":
54
data = "#cloud-config\n"
55
name = 'ssh_authorized_keys'
58
data += name + ": " + jdump(keys) + "\n"
60
return jdump(keys) + "\n"
65
parser = argparse.ArgumentParser(
66
description='Dump users public ssh keys in a variety of formats')
67
parser.add_argument('mode', nargs='?', help='output mode',
68
choices=VALID_MODES, default='dump')
69
args = parser.parse_args()
70
sys.stdout.write(format_pubkeys(args.mode, read_pubkeys()))
73
if __name__ == '__main__':