From 921c51e118f62c6e7de3ca2e6f22483565f4f0f6 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Fri, 4 Sep 2020 14:48:23 +0200 Subject: [PATCH] match IPs more robustly and multiple per line --- respodns/ip_util.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/respodns/ip_util.py b/respodns/ip_util.py index 0e1d106..34d96ec 100644 --- a/respodns/ip_util.py +++ b/respodns/ip_util.py @@ -1,17 +1,24 @@ import re ipv4_pattern = re.compile(r"(\d+)\.(\d+)\.(\d+)\.(\d+)", re.ASCII) +ipv4_pattern_strict = re.compile(r""" +(?:^|(?<=[^\d.])) +(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?) +\. +(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?) +\. +(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?) +\. +(0|1\d\d|2[0-4]\d|25[0-5]|[1-9]\d?) +(?:$|(?=[^\d.])) +""", re.ASCII | re.VERBOSE) def read_ips(f): - # TODO: make more robust. (regex pls) - for ip in f.readlines(): - if "#" in ip: - ip, _, _ = ip.partition("#") - ip = ip.strip() - if ip.count(".") != 3: - continue - yield ip + for line in f.readlines(): + line, _, _ = line.partition("#") # ignore comments + for match in ipv4_pattern_strict.finditer(line): + yield match.group() # yield the entire string def addr_to_int(ip):