Files
hse-2026/materials/07-naming-lookup/seminar/dns/control.py
T
2026-10-21 23:03:28 +03:00

26 lines
1.2 KiB
Python

"""Change the authoritative record or flush the resolver's cache."""
import argparse
import ipaddress
import json
from pathlib import Path
import subprocess
from server import ROOT, write_zone
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("action", choices=["reset", "update", "flush", "status"])
parser.add_argument("--address", default="192.0.2.20")
parser.add_argument("--ttl", type=int)
args = parser.parse_args()
if args.ttl is not None and not 1 <= args.ttl <= 3600:
parser.error("TTL must be between 1 and 3600")
if args.action in ("reset", "update"):
previous = json.loads((ROOT / "state.json").read_text())
address = "192.0.2.10" if args.action == "reset" else str(ipaddress.IPv4Address(args.address))
ttl = args.ttl if args.ttl is not None else (60 if args.action == "reset" else previous["ttl"])
write_zone(address, ttl)
subprocess.run(["named-checkzone", "lab.test", str(ROOT / "lab.test.zone")], check=True)
subprocess.run(["rndc", "-c", str(ROOT / "rndc.conf"), "reload", "lab.test"], check=True)
print((ROOT / "state.json").read_text())
else:
subprocess.run(["rndc", "-c", str(ROOT / "rndc.conf"), args.action], check=True)