From 60301b07b523d8d7d26a6c200c053ec134d25607 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 15 Sep 2022 19:49:49 -0400 Subject: [PATCH] bin/djbdns-logparse.py: use f-strings for all string interpolation. --- bin/djbdns-logparse.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/bin/djbdns-logparse.py b/bin/djbdns-logparse.py index 8aa1837..e955183 100755 --- a/bin/djbdns-logparse.py +++ b/bin/djbdns-logparse.py @@ -99,7 +99,7 @@ def convert_ip(ip : str): """ if len(ip) == 8: # IPv4, eg. "7f000001" -> "7f 00 00 01" -> "127.0.0.1" - return "%d.%d.%d.%d" % tuple(pack(">L", int(ip, 16))) + return ".".join(map(str, pack(">L", int(ip, 16)))) elif len(ip) == 32: # IPv6 is actually simpler -- it's just a string-slicing operation. return ":".join([ip[(4*i) : (4*i+4)] for i in range(8)]) @@ -107,22 +107,25 @@ def convert_ip(ip : str): def decode_client(words, i): chunks = words[i].split(":") - if len(chunks) == 2: # ip:port - words[i] = "%s:%d" % (convert_ip(chunks[0]), int(chunks[1], 16)) - elif len(chunks) == 3: - words[i] = "%s:%d (id %d)" % (convert_ip(chunks[0]), - int(chunks[1], 16), - int(chunks[2], 16)) + + ip = convert_ip(chunks[0]) + port = int(chunks[1], 16) + words[i] = f"{ip}:{port}" + + if len(chunks) == 3: + # For a "query" entry's clientip:clientport:id field. + id = int(chunks[2], 16) + words[i] += f" (id {id})" def decode_ip(words, i): words[i] = convert_ip(words[i]) def decode_ttl(words, i): - words[i] = "TTL=%s" % words[i] + words[i] = f"TTL={words[i]}" def decode_serial(words, i): serial = int(words[i]) - words[i] = "#%d" % serial + words[i] = f"#{serial}" def decode_type(words, i): qt = words[i] @@ -221,19 +224,20 @@ def handle_dnscache_log(line) -> typing.Optional[str]: chars = [] for i in range(1, len(response)//2): chars.append(chr(int(response[2*i : (2*i)+2], 16))) - words[4] = "%d:\"%s%s\"" % (length, "".join(chars), ellipsis) + txt = "".join(chars) + words[4] = f"{length}:\"{txt}{ellipsis}\"" elif event == "sent": decode_serial(words, 0) elif event == "stats": - words[0] = "count=%s" % words[0] - words[1] = "motion=%s" % words[1] - words[2] = "udp-active=%s" % words[2] - words[3] = "tcp-active=%s" % words[3] + words[0] = f"count={words[0]}" + words[1] = f"motion={words[1]}" + words[2] = f"udp-active={words[2]}" + words[3] = f"tcp-active={words[3]}" elif event == "tx": - words[0] = "g=%s" % words[0] + words[0] = f"g={words[0]}" decode_type(words, 1) # words[2] = name # words[3] = control (domain for which these servers are believed -- 2.43.2