Have own format for data read from .ssh/known_hosts, in case we want to work on it
This commit is contained in:
parent
3a9c4baa4b
commit
1a3257c870
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue