#!/usr/bin/perl use strict; use warnings; my $inFile = shift || '/var/cache/bind/cancerprev.org.db'; my $outFile = shift || '/var/cache/bind/db.88.189.146'; my $ipPrefix = shift; if (! defined $ipPrefix) { if ($outFile =~ /db\.([\d\.]+)$/) { $ipPrefix = join('.', reverse(split(/\./, $1))); print "Using autodetected IP prefix: $ipPrefix.\n"; } else { die "No IP prefix specified, and input filename not db.0.168.192 form.\n"; } } my $hostRe = qr/[[:alnum:].\-]+/; #my $ipRe = qr/\d{1,3}(?:\.\d{1,3}){3}/; my $lineRe = qr/^($hostRe)\s+IN\s+A\s+\Q$ipPrefix\E\.([\d\.]+)\s*$/; my %ipMap; open(IN, '<', $inFile) or die($!); while () { if ($_ =~ $lineRe) { # For IPs that have multiple aliases, the first alias is used. if (! defined $ipMap{$2}) { $ipMap{$2} = $1; } else { print "Skipping $1 ($2 is already mapped to $ipMap{$2})\n"; } } } close IN; my $serialNum = sprintf '%10d', (time()); # yeah, i know this is a bit of a hack... my $template = <', $outFile or die $!; print OUT $template; foreach my $ip (keys %ipMap) { printf OUT "%s IN PTR %s.office.cancerprev.org.\n", ($ip, $ipMap{$ip}); } close OUT;