From a5b6dfbb8538cb714c9f1d0b17f1ee415a6f483f Mon Sep 17 00:00:00 2001 From: Minoplhy Date: Thu, 12 Aug 2021 17:38:10 +0700 Subject: [PATCH] Regex attempt 4 --- to-rpz/host_rpz_argv.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/to-rpz/host_rpz_argv.py b/to-rpz/host_rpz_argv.py index eaa2d51..4d92458 100644 --- a/to-rpz/host_rpz_argv.py +++ b/to-rpz/host_rpz_argv.py @@ -9,13 +9,16 @@ outfile = sys.argv[2] with open(infile) as f: file = f.read().split('\n') for i in range(len(file)): - file[i] = re.sub('^0.0.0.0 0.0.0.0', '', file[i]) + file[i] = re.sub('0.0.0.0$', '', file[i]) file[i] = re.sub('^127.0.0.1 ', '', file[i]) file[i] = re.sub('^0.0.0.0 ', '', file[i]) file[i] = re.sub('^0 ', '', file[i]) file[i] = re.sub('^:: ', '', file[i]) file[i] = re.sub('^::1 ', '' ,file[i]) - file[i] = re.sub('^127.0.0.1', '', file[i]) + file[i] = re.sub('127.0.0.1$', '', file[i]) + file[i] = re.sub('0$', '', file[i]) + file[i] = re.sub('::$', '', file[i]) + file[i] = re.sub('::1$', '' ,file[i]) with open(infile, 'w') as f1: f1.writelines(["%s\n" % item for item in file]) f.close() @@ -48,10 +51,10 @@ f.close() remove_words = ['localhost','localhost.localdomain','local','broadcasthost','loopback','ip6-localnet','ip6-mcastprefix','ip6-allnodes','ip6-allrouters','ip6-allhosts','ip6-loopback'] with open(infile, 'r') as f: - lines = f.read().splitlines() -with open(infile, 'w') as f: + lines = f.read().splitlines() +with open(outfile, 'w') as f: for line in lines: - if not any(remove_word in line for remove_word in remove_words): + if not line.endswith((tuple(remove_words))): f.write('\n'.join([line + '\n'])) with open(infile) as f: