You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible/public_keys/dns_zone.py

40 lines
983 B
Python

#!/usr/bin/env python3
from pathlib import Path
import sys
class DnsRootNoParentError(Exception):
pass
def get_dns_parent(domain):
s = domain.split('.', 1)
if len(s) < 2:
raise DnsRootNoParentError()
return domain.split('.', 1)[1]
def find_dns_zone(map_dir, domain):
dns_file = Path(map_dir) / domain
if dns_file.exists():
return domain
else:
return find_dns_zone(map_dir, get_dns_parent(domain))
def main():
dns_map_dir = Path(sys.argv[0]).parent / "dns"
if len(sys.argv) >= 1:
domains = sys.argv[1:]
else:
domains = []
for domain in sys.stdin:
domains.append(domain.strip())
for domain in domains:
domain = domain.strip('.')
try:
print(find_dns_zone(dns_map_dir, domain))
except DnsRootNoParentError:
print(f'No dns zone found for "{domain}"', file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()