Skip to content
Snippets Groups Projects
Commit 802a30cb authored by Nicolas KAROLAK's avatar Nicolas KAROLAK
Browse files

parse systemd-resolve output

parent 419f8cfb
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ Checks that DNS records are provided by the customer servers are correctly set ...@@ -6,6 +6,7 @@ Checks that DNS records are provided by the customer servers are correctly set
""" """
from pathlib import Path from pathlib import Path
import re
import subprocess import subprocess
import sys import sys
...@@ -16,15 +17,33 @@ from envsetup import utils as u # noqa: E402 ...@@ -16,15 +17,33 @@ from envsetup import utils as u # noqa: E402
def get_dns_servers() -> set: def get_dns_servers() -> set:
with open("/etc/resolv.conf", "r") as f: if subprocess.getstatusoutput("command -v nmcli")[0] == 0:
d = f.read().strip() # network-manager
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
_, output = subprocess.getstatusoutput( _, output = subprocess.getstatusoutput(
"nmcli -f all device show | grep IP4.DNS | awk '{ print $2 }'" "nmcli -f all device show | grep IP4.DNS | awk '{ print $2 }'"
) )
servers = output.split("\n") 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) return set(servers)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment