#!/usr/bin/env python3 import subprocess import os 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(zone): 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 ""