Compare commits

...

1 Commits

Author SHA1 Message Date
link2xt
e9456be5bc Use dig +short option to simplify DNS parsing
Without this option parsing of answer was flaky
as for long records like
_submission._tcp.nine.testrun.org.
dig printed the result with a space rather
than tab as a separator and .split("\t") did not work.

This change makes the `dig` command print the answer
in the form we need so there is no need for complex parsing
other than taking the first line.

`-r` option is added to make sure options are not changed by .digrc
in the root home directory.
2023-12-22 12:48:20 +00:00

View File

@@ -37,21 +37,15 @@ class DNS:
def get(self, typ: str, domain: str) -> str | None: def get(self, typ: str, domain: str) -> str | None:
"""Get a DNS entry""" """Get a DNS entry"""
dig_result = self.shell(f"dig {typ} {domain}") dig_result = self.shell(f"dig -r -q {domain} -t {typ} +short")
line_num = 0 line = dig_result.partition("\n")[0]
for line in dig_result.splitlines(): if line:
line_num += 1 return line
if line.strip() == ";; ANSWER SECTION:":
return dig_result.splitlines()[line_num].split("\t")[-1]
def check_ptr_record(self, ip: str, mail_domain) -> str: def check_ptr_record(self, ip: str, mail_domain) -> bool:
"""Check the PTR record for an IPv4 or IPv6 address.""" """Check the PTR record for an IPv4 or IPv6 address."""
result = self.get("-x", ip) result = self.shell(f"dig -r -x {ip} +short").rstrip()
if result: return result == f"{mail_domain}."
if ip_address(ip).version == 6:
result = result.split()[-1]
if result[:-1] == mail_domain:
return result
def show_dns(args, out): def show_dns(args, out):