[dns-operations] Tool to store zone file with TYPExxx records

Matthew Pounsett matt at conundrum.com
Thu Apr 4 22:26:54 UTC 2019


On Thu, 4 Apr 2019 at 17:55, Matthew Pounsett <matt at conundrum.com> wrote:
>
>
> I'm not aware of an existing tool that could do this, but if you're familiar with Python, I suspect it would be possible to write something by fiddling with the presentation format code in dnspython.  I think all you'd have to do is monkeypatch dns.rdatatype.to_text() so that it never makes the call to _by_value.get(value).


I was curious how hard this would be, so went ahead and wrote it. :)

The conversion from text to a zone object depends on the
rdatatype.to_text() method returning a known mnemonic, so you have to
do the monkeypatch after you've got the zone object.   The code below
works in python3.

   #!/usr/bin/env python

   import argparse
   import dns.zone
   import dns.rdatatype


   def my_to_text(value):
       """
       Convert a DNS rdata type value to TYPE#### representation.  Skip the
       lookup for known mnemonics.

       Raises ``ValueError`` if the rdata type value is not >= 0 and <= 65535.

       Returns a ``str``.
       """

       if value < 0 or value > 65535:
           raise ValueError("type must be between >= 0 and <= 65535")
       text = 'TYPE' + repr(value)
       return text


   if __name__ == '__main__':
       parser = argparse.ArgumentParser(
           description=(
               "Convert the type mnemonics in a zone file to "
               "TYPE### representation."
           )
       )
       parser.add_argument('-f', '--file', help="zone file", required=True)
       parser.add_argument('-z', '--zone', help="zone name", required=True)
       args = parser.parse_args()

       zone = dns.zone.from_file(args.file, args.zone)

       dns.rdatatype.to_text = my_to_text

       print(zone.to_text().decode())




More information about the dns-operations mailing list