scripts-misc/powerdns/findzone.py

29 lines
981 B
Python
Raw Normal View History

2023-03-31 10:42:21 +02:00
#!/usr/bin/env python3
import subprocess
import os
2023-03-31 10:42:21 +02:00
def findzone(record : str) -> str:
"""Suggest best matching zone from existing zone
Proposal is done on longer zone names matching the record suffix.
Example::
record: a.test.sub.domain.tld
zone: domain.tld
zone: sub.domain.tld <== best match
zone: another.domain.tld
:param str record: Record name
:return: Zone as :class:`string` object or empty :class:`string`
"""
pdnsdomains = subprocess.check_output(["ssh", "-q", str(os.environ.get('PDNS_PRIMARY_SSH')), "pdnsutil", "list-all-zones", "2>", "/dev/null"]).decode('utf-8').split("\n")
best_match = None
for zone in pdnsdomains:
if record.endswith(f".{zone}"):
2023-03-31 10:42:21 +02:00
if not best_match:
best_match = zone
if best_match and len(zone) > len(best_match):
best_match = zone
return best_match if best_match else ""