From 802a30cb1934028c76ac4dedbaab1701344190ba Mon Sep 17 00:00:00 2001 From: Nicolas KAROLAK <nicolas@karolak.fr> Date: Tue, 15 Oct 2019 16:57:09 +0000 Subject: [PATCH] parse systemd-resolve output --- tests/test_dns_records.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/test_dns_records.py b/tests/test_dns_records.py index ede15430..8dd54cb9 100755 --- a/tests/test_dns_records.py +++ b/tests/test_dns_records.py @@ -6,6 +6,7 @@ Checks that DNS records are provided by the customer servers are correctly set """ from pathlib import Path +import re import subprocess import sys @@ -16,15 +17,33 @@ from envsetup import utils as u # noqa: E402 def get_dns_servers() -> set: - with open("/etc/resolv.conf", "r") as f: - d = f.read().strip() - servers = [l.split()[1] for l in d.split("\n") if l.startswith("nameserver")] - if len(servers) and servers[0].startswith("127.0."): - # network-manager or systemd-resolved + if subprocess.getstatusoutput("command -v nmcli")[0] == 0: + # network-manager _, output = subprocess.getstatusoutput( "nmcli -f all device show | grep IP4.DNS | awk '{ print $2 }'" ) servers = output.split("\n") + else: + # resolvconf + with open("/etc/resolv.conf", "r") as f: + d = f.read().strip() + servers = [l.split()[1] for l in d.split("\n") if l.startswith("nameserver")] + + if "127.0.0.53" in servers: + # systemd-resolved + _, output = subprocess.getstatusoutput("systemd-resolve --status") + lines = [l.strip() for l in output.split("\n")] + dns_line = False + ip_pattern = re.compile("\d+\.\d+\.\d+\.\d+") + for line in lines: + if line.startswith("DNS Servers:"): + dns_line = True + servers.append(line.split()[-1]) + elif dns_line and ip_pattern.match(line): + servers.append(line) + else: + dns_line = False + return set(servers) -- GitLab