Question

I am trying to write a script that will take in a BIND zone file, grab all of the A records, in the format host ip. I've done that by grep -w 'A' "$A_ZONE"|awk '{print $1,$4}'|sort -V, to skip the IN A part. Now, I need to extract PTR records from all of the reverse zones that I have. Those are grouped by /24 subnets, so if I have a PTR record for 10.0.0.1, it would be in the 0.0.10.in-addr.arpa.zone file, as 10 IN PTR host.domain.tld. Seeing as that is a bit convoluted, I'm not sure how to extract the IP well, so that it would be in the format of the first file that I extracted, host ip.

Any suggestions?

Était-ce utile?

La solution

You can use the following command:

egrep '^[0-9]+' 0.0.10.in-addr.arpa.zone | \
  perl -p -e 's/^(\d+).*\s(\S+)\s*$/$2 10.0.0.$1/'

Output:

host.domain.tld. 10.0.0.10

It greps all the records that start with a number, and match the number and hostname and reverse them. The IP address is then constructed along with the hostname.

Note that in the command I showed, the subnet is hardcoded in the regexp, but you could apply similar strategy to extract it from your filename plug it into the regex.

You may also want to consider running your zone files through named-compilezone so as to make sure that they are in a canonical format suitable for scripting.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top