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
|
# TODO: Add a switch for whether you want to include IPs
|
||||||
with open(sys.argv[1], 'r') as f:
|
with open(sys.argv[1], 'r') as f:
|
||||||
try:
|
try:
|
||||||
entries = process_known_hosts.process_file(f)
|
known_host_entries = process_known_hosts.process_file(f)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
print('Error: %s' % err, file=sys.stderr)
|
print('Error: %s' % err, file=sys.stderr)
|
||||||
sys.exit(1)
|
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
|
# Write to stdout by default
|
||||||
# TODO: Add a way to change it
|
# TODO: Add a way to change it
|
||||||
# We use sys.stdout.buffer instead of just sys.stdout because we
|
# We use sys.stdout.buffer instead of just sys.stdout because we
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
import entry
|
import entry
|
||||||
|
|
||||||
|
KnownHostsEntry = namedtuple('KnownHostsEntry', ['domain', 'port', 'fingerprint'])
|
||||||
|
|
||||||
class KnownHostsSyntaxError(Exception):
|
class KnownHostsSyntaxError(Exception):
|
||||||
def __init__(self, string):
|
def __init__(self, string):
|
||||||
self.string = string
|
self.string = string
|
||||||
|
@ -75,8 +79,7 @@ def is_ip(domain):
|
||||||
return is_ipv4(domain) or is_ipv6(domain)
|
return is_ipv4(domain) or is_ipv6(domain)
|
||||||
|
|
||||||
def process_line(line, ignore_ips):
|
def process_line(line, ignore_ips):
|
||||||
# TODO: Add a way to skip IPs
|
"""process_line(str, bool) → [KnownHostsEntry]
|
||||||
"""process_line(str, bool) → [Entry]
|
|
||||||
Given a string containing one line of .ssh/known_hosts file, create
|
Given a string containing one line of .ssh/known_hosts file, create
|
||||||
a list of Entries based on it.
|
a list of Entries based on it.
|
||||||
|
|
||||||
|
@ -110,7 +113,7 @@ def process_line(line, ignore_ips):
|
||||||
fingerprint = m.digest()
|
fingerprint = m.digest()
|
||||||
|
|
||||||
# There can be several hosts separated with a comma
|
# There can be several hosts separated with a comma
|
||||||
entries = []
|
known_host_entries = []
|
||||||
for host in hosts.split(','):
|
for host in hosts.split(','):
|
||||||
# A host can't be empty
|
# A host can't be empty
|
||||||
if len(host) == 0:
|
if len(host) == 0:
|
||||||
|
@ -145,13 +148,12 @@ def process_line(line, ignore_ips):
|
||||||
if ignore_ips and is_ip(domain):
|
if ignore_ips and is_ip(domain):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Default to no comment
|
known_host_entries.append(KnownHostsEntry(domain, port, fingerprint))
|
||||||
entries.append(entry.create_entry(domain, port, fingerprint, ''))
|
|
||||||
|
|
||||||
return entries
|
return known_host_entries
|
||||||
|
|
||||||
def process_file(f, ignore_ips = True):
|
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
|
Given a file in the .ssh/known_hosts format, create a list of
|
||||||
entries.
|
entries.
|
||||||
|
|
||||||
|
@ -167,3 +169,12 @@ def process_file(f, ignore_ips = True):
|
||||||
raise err
|
raise err
|
||||||
|
|
||||||
return entries
|
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