Update #! line to Python 3
[cacert-infradocs.git] / tools / ssh_host_keys.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import os.path
5 import subprocess
6 from glob import glob
7
8 SUPPORTED_SSH_KEY_TYPES = ('RSA', 'DSA', 'ECDSA', 'ED25519')
9
10
11 if __name__ == '__main__':
12 parser = argparse.ArgumentParser(
13 description=(
14 'Convert a set of ssh host keys to the syntax expected by the '
15 'sshkeys directive of the CAcert infrastructure documentation'))
16 parser.add_argument(
17 'root', metavar='ROOT', type=str, help='root directory'
18 )
19 args = parser.parse_args()
20
21 keys = {}
22 for host_key in glob(os.path.join(
23 args.root, 'etc/ssh', 'ssh_host_*key.pub')
24 ):
25 fp = subprocess.check_output(
26 ['ssh-keygen', '-l', '-f', host_key]).strip().split()
27 keys[fp[3][1:-1].decode('ascii')] = fp[1].decode('ascii')
28
29 max_length = max([len(key) for key in keys.keys()
30 if key in SUPPORTED_SSH_KEY_TYPES])
31
32 print(".. sshkeys::")
33 for typ, key in [
34 (typ, keys[typ]) for typ in SUPPORTED_SSH_KEY_TYPES
35 if typ in keys
36 ]:
37 print(" :{}:{} {}".format(typ, ' ' * (max_length - len(typ)), key))