From 7754f17cc90663c1f809d0cadca0909cf2e9af30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 1 Sep 2018 21:55:09 +0300 Subject: [PATCH] Use .sshwot/*.sshwot by default in sshwot-filter --- Makefile | 2 +- src/main-filter.py | 10 ++++++++-- src/open_default_files.py | 24 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/open_default_files.py diff --git a/Makefile b/Makefile index 4cb4933..1f6ce78 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ SSHWOT_EXPORT_KNOWN_HOSTS_MAIN:=src/main-export-known-hosts.py SSHWOT_EXPORT_KNOWN_HOSTS_DEPS:=src/entry.py src/hashing.py src/process_known_hosts.py src/write_file.py SSHWOT_FILTER_MAIN:=src/main-filter.py -SSHWOT_FILTER_DEPS:=src/entry.py src/hashing.py src/read_file.py src/write_file.py +SSHWOT_FILTER_DEPS:=src/entry.py src/hashing.py src/open_default_files.py src/read_file.py src/write_file.py all: $(BINS) diff --git a/src/main-filter.py b/src/main-filter.py index 11f9a25..6b9850a 100644 --- a/src/main-filter.py +++ b/src/main-filter.py @@ -3,11 +3,11 @@ import sys import entry import hashing +import open_default_files import read_file import write_file def main(): - # TODO: Default location to search parser = argparse.ArgumentParser( description = """Search sshwot file(s) for given host and/or fingerprint.""", @@ -94,8 +94,14 @@ def main(): else: fingerprint = None + # Use the default files if no input files were specified + if len(args.infiles) == 0: + infiles = open_default_files.open_all() + else: + infiles = args.infiles + matches = [] - for infile in args.infiles: + for infile in infiles: entries, file_comment = read_file.read(infile) # Filter by host if it's present diff --git a/src/open_default_files.py b/src/open_default_files.py new file mode 100644 index 0000000..0c39ae3 --- /dev/null +++ b/src/open_default_files.py @@ -0,0 +1,24 @@ +import os + +def open_all(): + """open_all() → [file(rb)] + Open the default sshwot files""" + + try: + homedir = os.environ['HOME'] + except KeyError as err: + raise KeyError('$HOME is not set') from err + + # If the directory doesn't exist, just return empty + try: + sshwot_dir = os.listdir(os.path.join(homedir, '.sshwot')) + except FileNotFoundError: + return [] + + # Read all the .sshwot files from /.sshwot by default + files = [] + for dir_entry in sshwot_dir: + if dir_entry.split('.')[-1] == 'sshwot': + files.append(open(dir_entry, 'rb')) + + return files