Filip Pytloun | 3668cd0 | 2015-11-18 16:16:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import base64 |
| 4 | import hashlib |
| 5 | |
| 6 | |
| 7 | def main(): |
| 8 | host_keys = { |
| 9 | 'rsa': '/etc/ssh/ssh_host_rsa_key.pub', |
| 10 | 'dsa': '/etc/ssh/ssh_host_dsa_key.pub', |
| 11 | 'ecdsa': '/etc/ssh/ssh_host_ecdsa_key.pub', |
| 12 | } |
| 13 | |
| 14 | ssh_fingerprints = {} |
| 15 | for key_type, filename in host_keys.iteritems(): |
| 16 | try: |
| 17 | ssh_fingerprints[key_type] = _get_ssh_fingerprint(filename) |
| 18 | except IOError: |
| 19 | pass |
| 20 | |
| 21 | if ssh_fingerprints: |
| 22 | return { |
| 23 | 'ssh_fingerprints': ssh_fingerprints |
| 24 | } |
| 25 | else: |
| 26 | return None |
| 27 | |
| 28 | |
| 29 | def _get_ssh_fingerprint(filename): |
| 30 | with open(filename, 'r') as fh: |
| 31 | key = base64.b64decode(fh.read().strip().split()[1].encode('ascii')) |
| 32 | fp_plain = hashlib.md5(key).hexdigest() |
| 33 | return ':'.join(a+b for a, b in zip(fp_plain[::2], fp_plain[1::2])) |