From 1a3257c870f0edeef16dd1d097f84769133e400b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Fri, 31 Aug 2018 12:17:16 +0300 Subject: [PATCH] Have own format for data read from .ssh/known_hosts, in case we want to work on it --- src/main-export-known-hosts.py | 7 ++++++- src/process_known_hosts.py | 25 ++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main-export-known-hosts.py b/src/main-export-known-hosts.py index 54ece35..2e37bc9 100644 --- a/src/main-export-known-hosts.py +++ b/src/main-export-known-hosts.py @@ -8,11 +8,16 @@ def main(): # TODO: Add a switch for whether you want to include IPs with open(sys.argv[1], 'r') as f: try: - entries = process_known_hosts.process_file(f) + known_host_entries = process_known_hosts.process_file(f) except Exception as err: print('Error: %s' % err, file=sys.stderr) sys.exit(1) + # Convert to the entry format for .sshwot files + entries = [] + for known_hosts_entry in known_host_entries: + entries.append(process_known_hosts.known_hosts_to_entry(known_hosts_entry)) + # Write to stdout by default # TODO: Add a way to change it # We use sys.stdout.buffer instead of just sys.stdout because we diff --git a/src/process_known_hosts.py b/src/process_known_hosts.py index ebe9a7b..7a16e72 100644 --- a/src/process_known_hosts.py +++ b/src/process_known_hosts.py @@ -1,8 +1,12 @@ import base64 import hashlib +from collections import namedtuple + import entry +KnownHostsEntry = namedtuple('KnownHostsEntry', ['domain', 'port', 'fingerprint']) + class KnownHostsSyntaxError(Exception): def __init__(self, string): self.string = string @@ -75,8 +79,7 @@ def is_ip(domain): return is_ipv4(domain) or is_ipv6(domain) def process_line(line, ignore_ips): - # TODO: Add a way to skip IPs - """process_line(str, bool) → [Entry] + """process_line(str, bool) → [KnownHostsEntry] Given a string containing one line of .ssh/known_hosts file, create a list of Entries based on it. @@ -110,7 +113,7 @@ def process_line(line, ignore_ips): fingerprint = m.digest() # There can be several hosts separated with a comma - entries = [] + known_host_entries = [] for host in hosts.split(','): # A host can't be empty if len(host) == 0: @@ -145,13 +148,12 @@ def process_line(line, ignore_ips): if ignore_ips and is_ip(domain): continue - # Default to no comment - entries.append(entry.create_entry(domain, port, fingerprint, '')) + known_host_entries.append(KnownHostsEntry(domain, port, fingerprint)) - return entries + return known_host_entries def process_file(f, ignore_ips = True): - """process_file(file(r), bool) → [Entry] + """process_file(file(r), bool) → [KnownHostsEntry] Given a file in the .ssh/known_hosts format, create a list of entries. @@ -167,3 +169,12 @@ def process_file(f, ignore_ips = True): raise err return entries + +def known_hosts_to_entry(known_hosts_entry, comment = ''): + """known_hosts_to_entry(KnownHostsEntry, str) → Entry + Converts an entry that's been read from known_hosts to one that can + be written to a .sshwot file""" + domain = known_hosts_entry.domain + port = known_hosts_entry.port + fingerprint = known_hosts_entry.fingerprint + return entry.create_entry(domain, port, fingerprint, comment)