- #!/usr/bin/perl -w
- use strict;
- use YERMOM;
- package ZoneParser;
- sub new {
- my $class = shift;
- my $self = {
- DNSPrimaryns => 'ns1.ibm.com.',
- DNSAdminEmail => 'noc.ibm.com.',
- DNSSerial => '2006122001',
- DNSRefresh => '3600',
- DNSRetry => '3600',
- DNSExpire => '1209600',
- DNSMinimum => '3600',
- SOADNSTTL => '86400'
- };
- bless $self, $class;
- return $self;
- }
- sub parse {
- my $self = shift;
- my $Filename = shift;
- open(F, "<$Filename") or die("Can not open $Filename for reading: $!\n");
- # Slup in the zone header...
- my $OldIRS = $/;
- $/ = "\t)\n";
- my $Header = <F>;
- $/ = $OldIRS;
- chomp($Header);
- my ($CurrentTTL, $CurrentZone);
- my $CurrentHost = '@';
- if ($Header =~ /\$TTL (\d+).*?\n(\S+)\s+IN\s+SOA\s+(\S+)\s+(\S+).*\n\s+(\d+).*\n\s+(\d+).*\n\s+(\d+).*\n\s+(\d+).*\n\s+(\d+)/is) {
- ($CurrentTTL, $CurrentZone, $self->{DNSPrimaryns}, $self->{DNSAdminEmail}, $self->{DNSSerial}, $self->{DNSRefresh}, $self->{DNSRetry}, $self->{DNSExpire}, $self->{DNSMinimum}) = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
- $self->{SOADNSTTL} = $CurrentTTL;
- } else {
- die("Invalid Zone Header...\n$Header\n\n");
- }
- while (<F>) {
- chomp();
- # Strip Comments...
- s/\s+\;.+$//;
- if (/^(\S+|\s)\s+(NS|A|MX|TXT|RP|CNAME)\s+(.+)$/i) {
- my ($Record, $Type, $Data) = ($1, lc($2), $3);
- if ($Record =~ /^\S+/) {
- $CurrentHost = $Record;
- }
- if ($Type eq 'mx') {
- my ($Priority, $Host) = split(' ',$Data);
- $self->printMXRecord($CurrentHost, $CurrentZone, $Host, $CurrentTTL, $Priority);
- } elsif ($Type eq 'a') {
- $self->printARecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
- } elsif ($Type eq 'ns') {
- $self->printNSRecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
- } elsif ($Type eq 'txt') {
- $self->printTXTRecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
- } elsif ($Type eq 'rp') {
- $self->printRPRecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
- } elsif ($Type eq 'cname') {
- $self->printCNAMERecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
- }
- } elsif (/^\$(\S+)\s+(.+?)\.?$/) {
- my ($Param, $Value) = ($1, $2);
- $Value =~ s/\.$//;
- $CurrentTTL = $Value if ($Param eq 'TTL');
- $CurrentZone = $Value if ($Param eq 'ORIGIN');
- }
- }
- close(F);
- }
- sub printZone {
- my ($self, $DNSZoneName) = @_;
- if ($self->{pritnedZones}{$DNSZoneName}++ < 1) {
- print <<EOF;
- # Zone {$DNSZoneName.}
- dn: DNSZoneName=$DNSZoneName,$self->{basedn}
- objectClass: DNSZone
- DNSZoneName: $DNSZoneName
- EOF
- }
- }
- sub printContainer {
- my ($self, $DNSHostName, $DNSZoneName) = @_;
- if ($self->{printedContainers}{$DNSZoneName}{$DNSHostName}++ < 1) {
- print <<EOF;
- # Container {$DNSHostName.$DNSZoneName.}
- dn: DNSHostName=$DNSHostName,DNSZoneName=$DNSZoneName,$self->{basedn}
- objectClass: DNSHost
- DNSHostName: $DNSHostName
- EOF
- }
- }
- sub printSOA {
- my ($self, $DNSHostName, $DNSZoneName, $DNSPrimaryns, $DNSAdminEmail, $DNSSerial, $DNSRefresh, $DNSRetry, $DNSExpire, $DNSMinimum, $DNSTTL) = @_;
- if ($self->{printedSOAs}{$DNSZoneName}{$DNSHostName}++ < 1) {
- print <<EOF;
- # SOA Record {$DNSHostName.$DNSZoneName.}
- dn: DNSRecord=SOA,DNSHostName=$DNSHostName,DNSZoneName=$DNSZoneName,$self->{basedn}
- objectClass: DNSSOARecord
- DNSHostName: $DNSHostName
- DNSRecord: SOA
- DNSType: soa
- DNSSerial: $DNSSerial
- DNSRefresh: $DNSRefresh
- DNSRetry: $DNSRetry
- DNSExpire: $DNSExpire
- DNSMinimum: $DNSMinimum
- DNSAdminEmail: $DNSAdminEmail
- DNSPrimaryns: $DNSPrimaryns
- DNSTTL: $DNSTTL
- EOF
- }
- }
- sub printARecord {
- my ($self, $DNSHostName, $DNSZoneName, $IPAddress, $DNSTTL) = @_;
- my $Block = "DNSIPAddr: $IPAddress";
- $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSARecord', 'a', 'A', $DNSTTL);
- }
- sub printMXRecord {
- my ($self, $DNSHostName, $DNSZoneName, $MX, $DNSTTL, $Priority) = @_;
- if (!$Priority) {
- $Priority = 1;
- }
- my $Block = "DNSData: $MX\nDNSPreference: $Priority";
- $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSMXRecord', 'mx', 'MX', $DNSTTL);
- }
- sub printTXTRecord {
- my ($self, $DNSHostName, $DNSZoneName, $TXT, $DNSTTL) = @_;
- my $Block = "DNSData: $TXT";
- if ($TXT =~ /v=spf/i) {
- $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSTEXTRecord', 'txt', 'SPF', $DNSTTL);
- } else {
- $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSTEXTRecord', 'txt', 'TXT', $DNSTTL);
- }
- }
- sub printNSRecord {
- my ($self, $DNSHostName, $DNSZoneName, $NS, $DNSTTL) = @_;
- my $Block = "DNSData: $NS";
- $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSNSRecord', 'ns', 'NS', $DNSTTL);
- }
- sub printRPRecord {
- my ($self, $DNSHostName, $DNSZoneName, $RP, $DNSTTL) = @_;
- my $Block = "DNSData: $RP";
- $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSGenericRecord', 'rp', 'RP', $DNSTTL);
- }
- sub printCNAMERecord {
- my ($self, $DNSHostName, $DNSZoneName, $CNAME, $DNSTTL) = @_;
- my $Block = "DNSData: $CNAME";
- $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSCNAMERecord', 'cname', 'CNAME', $DNSTTL);
- }
- sub _printRecord {
- my ($self, $DNSHostName, $DNSZoneName, $Block, $objectClass, $DNSType, $DNSRecord, $DNSTTL) = @_;
- $self->printZone($DNSZoneName);
- $self->printContainer($DNSHostName, $DNSZoneName);
- $self->printSOA('@', $DNSZoneName, $self->{DNSPrimaryns}, $self->{DNSAdminEmail}, $self->{DNSSerial}, $self->{DNSRefresh}, $self->{DNSRetry}, $self->{DNSExpire}, $self->{DNSMinimum}, $self->{SOADNSTTL});
- my $Count = $self->{printedRecords}{count}{$DNSZoneName}{$DNSHostName}{$DNSRecord} ++ ;
- $Count = $Count || '';
- $DNSRecord .= $Count;
- # Some records we want to have 0-n appended regardless as we're more likely to have more then one.
- if ($DNSType =~ /^(?:mx|ns|rp|txt)$/i && !($DNSRecord =~ /\d+$/)) {
- $DNSRecord .= '0';
- }
- print <<EOF;
- # $DNSRecord ($DNSType) Record {$DNSHostName.$DNSZoneName.}
- dn: DNSRecord=$DNSRecord,DNSHostName=$DNSHostName,DNSZoneName=$DNSZoneName,$self->{basedn}
- objectClass: $objectClass
- DNSType: $DNSType
- DNSRecord: $DNSRecord
- DNSHostName: $DNSHostName
- DNSTTL: $DNSTTL
- $Block
- EOF
- }
- package main;
- my $zone = ZoneParser->new();
- $zone->{basedn} = 'ou=dns,dc=ibm,dc=com';
- $zone->parse('ibm.com.hosts');
Posted by Anonymous on Wed 20th Dec 17:55 (modification of post by view diff)
diff | download | new post
Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.