asn-dns.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python
  2. """This script generates DNS TXT records containing ASN information.
  3. The idea comes from asn.cymru.com. Example:
  4. dig +short AS1.asn.cymru.com TXT
  5. The output looks like this:
  6. "1 | US | arin | 2001-09-20 | LVLT-1 - Level 3 Communications, Inc."
  7. In dn42, we try to respect this format. Fields 2 and 3 are set to "DN42"
  8. and "dn42", respectively. The date field is left empty, as we currently
  9. do not record this information in the registry. For the last field, we use
  10. "[as-name] [descr]" from the registry.
  11. """
  12. import sys
  13. import time
  14. from registry import AutNum
  15. REGISTRYPATH = "/home/zorun/net.dn42.registry"
  16. OUT = "/home/zorun/tmp/asn.dn42.zone"
  17. ORIGIN = "asn.dn42."
  18. TTL = "1h"
  19. NS = ["tuxmachine.polynome.dn42.", "ns1.allowed.dn42.", "ns1.pyropeter.dn42.", "beta.synhacx.dn42."]
  20. def gen_zone(registrypath, out):
  21. # SOA and stuff
  22. out.write("$ORIGIN {}\n".format(ORIGIN))
  23. out.write("$TTL {}\n".format(TTL))
  24. serial = int(time.time())
  25. out.write("@ IN SOA tuxmachine.polynome.dn42. dn42.polyno.me. ({} 1d 1h 1w 5m)\n".format(serial))
  26. # NS
  27. for ns in NS:
  28. out.write("@ NS {}\n".format(ns))
  29. # TXT data
  30. autnum = AutNum(registrypath)
  31. for (asn, data) in autnum.data.items():
  32. comment = ""
  33. if "as-name" in data:
  34. comment = data["as-name"][0]
  35. if "descr" in data:
  36. comment += " " + data["descr"][0]
  37. comment.replace('"', '')
  38. out.write('{} TXT "{} | DN42 | dn42 | | {}"\n'.format(asn, asn[2:], comment))
  39. if __name__ == "__main__":
  40. with open(OUT, "w") as f:
  41. gen_zone(REGISTRYPATH, f)