Skip to content

Instantly share code, notes, and snippets.

@eiro
Created November 1, 2010 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiro/658253 to your computer and use it in GitHub Desktop.
Save eiro/658253 to your computer and use it in GitHub Desktop.
parsing DNS zone with regexp::grammars
#! /usr/bin/perl
use Modern::Perl;
use YAML;
# Net::Grammars::Digzone;
# possible bugs: no empty line available
# http://en.wikipedia.org/wiki/List_of_DNS_record_types
my $parse_dig_zone = do {
use Regexp::Grammars;
qr{
\A (?: <[record]>
| <.comment=(?: [;][^\n]+\n )>
)+
\z
<rule:record>
<simplerecord>
| <srvrecord>
| <soarecord>
| <txtrecord>
# http://en.wikipedia.org/wiki/SRV_record#Record_format
<rule:srvrecord>
[_] <service=ident>
[.][_] <proto=(tcp | udp )>
[.] <name=hostname>
<ttl=num> IN SRV
<priority=num>
<weight=num>
<port=num>
<target=hostname>
\n
<rule:txtrecord>
<entry=hostname>
<ttl=num> IN TXT
\" <txt=(?:[^\n]+)> \" \s* \n
<rule:soarecord>
<entry=hostname>
<ttl=num> IN SOA
<contact=hostname>
<serial=num>
<refresh=num>
<retry=num>
<expiry=num>
<rrttl=num>
\n
<rule:simplerecord>
<entry=hostname>
<ttl=num> IN <simplerecordtype>
<value=hostname>
\n
<rule:mxrecord>
<entry=hostname>
<ttl=num> IN <simplerecordtype>
<value=hostname>
<priority=num>
\n
<rule:simplerecordtype> ( NS | CNAME | A | AAAA )
<rule:hostname> <.ident>(.<.ident>)*.?
<token:num> \d+
<token:ident> ( [-a-z0-9]+ | \* )
}xsm
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment