pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

paste-bin - collaborative debugging tool View Help


Posted by Anonymous on Wed 20th Dec 17:55 (modification of post by view diff)
diff | download | new post

  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4.  use YERMOM;
  5. package ZoneParser;
  6.  
  7.         sub new {
  8.                 my $class = shift;
  9.                 my $self = {
  10.                         DNSPrimaryns => 'ns1.ibm.com.',
  11.                         DNSAdminEmail => 'noc.ibm.com.',
  12.                         DNSSerial => '2006122001',
  13.                         DNSRefresh => '3600',
  14.                         DNSRetry => '3600',
  15.                         DNSExpire => '1209600',
  16.                         DNSMinimum => '3600',
  17.                         SOADNSTTL => '86400'
  18.                 };
  19.                 bless $self, $class;
  20.                 return $self;
  21.         }
  22.  
  23.         sub parse {
  24.                 my $self = shift;
  25.                 my $Filename = shift;
  26.                 open(F, "<$Filename") or die("Can not open $Filename for reading: $!\n");
  27.  
  28.                 # Slup in the zone header...
  29.                 my $OldIRS = $/;
  30.                 $/ = "\t)\n";
  31.                 my $Header = <F>;
  32.                 $/ = $OldIRS;
  33.                 chomp($Header);
  34.  
  35.                 my ($CurrentTTL, $CurrentZone);
  36.                 my $CurrentHost = '@';
  37.  
  38.                 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) {
  39.                         ($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);
  40.                         $self->{SOADNSTTL} = $CurrentTTL;
  41.                 } else {
  42.                         die("Invalid Zone Header...\n$Header\n\n");
  43.                 }
  44.  
  45.                 while (<F>) {
  46.                         chomp();
  47.                         # Strip Comments...
  48.                         s/\s+\;.+$//;
  49.  
  50.                         if (/^(\S+|\s)\s+(NS|A|MX|TXT|RP|CNAME)\s+(.+)$/i) {
  51.                                 my ($Record, $Type, $Data) = ($1, lc($2), $3);
  52.                                 if ($Record =~ /^\S+/) {
  53.                                         $CurrentHost = $Record;
  54.                                 }
  55.                                 if ($Type eq 'mx') {
  56.                                         my ($Priority, $Host) = split(' ',$Data);
  57.                                         $self->printMXRecord($CurrentHost, $CurrentZone, $Host, $CurrentTTL, $Priority);
  58.                                 } elsif ($Type eq 'a') {
  59.                                         $self->printARecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
  60.                                 } elsif ($Type eq 'ns') {
  61.                                         $self->printNSRecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
  62.                                 } elsif ($Type eq 'txt') {
  63.                                         $self->printTXTRecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
  64.                                 } elsif ($Type eq 'rp') {
  65.                                         $self->printRPRecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
  66.                                 } elsif ($Type eq 'cname') {
  67.                                         $self->printCNAMERecord($CurrentHost, $CurrentZone, $Data, $CurrentTTL);
  68.                                 }
  69.                         } elsif (/^\$(\S+)\s+(.+?)\.?$/) {
  70.                                 my ($Param, $Value) = ($1, $2);
  71.                                 $Value =~ s/\.$//;
  72.                                 $CurrentTTL = $Value if ($Param eq 'TTL');
  73.                                 $CurrentZone = $Value if ($Param eq 'ORIGIN');
  74.                         }
  75.                 }
  76.                 close(F);
  77.         }
  78.  
  79.  
  80.         sub printZone {
  81.                 my ($self, $DNSZoneName) = @_;
  82.  
  83.                 if ($self->{pritnedZones}{$DNSZoneName}++ < 1)  {
  84.                         print <<EOF;
  85. # Zone {$DNSZoneName.}
  86. dn: DNSZoneName=$DNSZoneName,$self->{basedn}
  87. objectClass: DNSZone
  88. DNSZoneName: $DNSZoneName
  89.  
  90. EOF
  91.                 }
  92.         }
  93.  
  94.  
  95.         sub printContainer {
  96.                 my ($self, $DNSHostName, $DNSZoneName) = @_;
  97.  
  98.                 if ($self->{printedContainers}{$DNSZoneName}{$DNSHostName}++ < 1) {
  99.                         print <<EOF;
  100. # Container {$DNSHostName.$DNSZoneName.}
  101. dn: DNSHostName=$DNSHostName,DNSZoneName=$DNSZoneName,$self->{basedn}
  102. objectClass: DNSHost
  103. DNSHostName: $DNSHostName
  104.  
  105. EOF
  106.                 }
  107.         }
  108.  
  109.         sub printSOA {
  110.                 my ($self, $DNSHostName, $DNSZoneName, $DNSPrimaryns, $DNSAdminEmail, $DNSSerial, $DNSRefresh, $DNSRetry, $DNSExpire, $DNSMinimum, $DNSTTL) = @_;
  111.  
  112.                 if ($self->{printedSOAs}{$DNSZoneName}{$DNSHostName}++ < 1) {
  113.                         print <<EOF;
  114. # SOA Record {$DNSHostName.$DNSZoneName.}
  115. dn: DNSRecord=SOA,DNSHostName=$DNSHostName,DNSZoneName=$DNSZoneName,$self->{basedn}
  116. objectClass: DNSSOARecord
  117. DNSHostName: $DNSHostName
  118. DNSRecord: SOA
  119. DNSType: soa
  120. DNSSerial: $DNSSerial
  121. DNSRefresh: $DNSRefresh
  122. DNSRetry: $DNSRetry
  123. DNSExpire: $DNSExpire
  124. DNSMinimum: $DNSMinimum
  125. DNSAdminEmail: $DNSAdminEmail
  126. DNSPrimaryns: $DNSPrimaryns
  127. DNSTTL: $DNSTTL
  128.  
  129. EOF
  130.                 }
  131.         }
  132.  
  133.         sub printARecord {
  134.                 my ($self, $DNSHostName, $DNSZoneName, $IPAddress, $DNSTTL) = @_;
  135.                 my $Block = "DNSIPAddr: $IPAddress";
  136.                 $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSARecord', 'a', 'A', $DNSTTL);
  137.         }
  138.  
  139.         sub printMXRecord {
  140.                 my ($self, $DNSHostName, $DNSZoneName, $MX, $DNSTTL, $Priority) = @_;
  141.                 if (!$Priority) {
  142.                         $Priority = 1;
  143.                 }
  144.                 my $Block = "DNSData: $MX\nDNSPreference: $Priority";
  145.                 $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSMXRecord', 'mx', 'MX', $DNSTTL);
  146.         }
  147.  
  148.         sub printTXTRecord {
  149.                 my ($self, $DNSHostName, $DNSZoneName, $TXT, $DNSTTL) = @_;
  150.                 my $Block = "DNSData: $TXT";
  151.                 if ($TXT =~ /v=spf/i) {
  152.                         $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSTEXTRecord', 'txt', 'SPF', $DNSTTL);
  153.                 } else {
  154.                         $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSTEXTRecord', 'txt', 'TXT', $DNSTTL);
  155.                 }
  156.         }
  157.  
  158.         sub printNSRecord {
  159.                 my ($self, $DNSHostName, $DNSZoneName, $NS, $DNSTTL) = @_;
  160.                 my $Block = "DNSData: $NS";
  161.                 $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSNSRecord', 'ns', 'NS', $DNSTTL);
  162.         }
  163.  
  164.         sub printRPRecord {
  165.                 my ($self, $DNSHostName, $DNSZoneName, $RP, $DNSTTL) = @_;
  166.                 my $Block = "DNSData: $RP";
  167.                 $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSGenericRecord', 'rp', 'RP', $DNSTTL);
  168.         }
  169.  
  170.         sub printCNAMERecord {
  171.                 my ($self, $DNSHostName, $DNSZoneName, $CNAME, $DNSTTL) = @_;
  172.                 my $Block = "DNSData: $CNAME";
  173.                 $self->_printRecord($DNSHostName, $DNSZoneName, $Block, 'DNSCNAMERecord', 'cname', 'CNAME', $DNSTTL);
  174.         }
  175.  
  176.         sub _printRecord {
  177.                 my ($self, $DNSHostName, $DNSZoneName, $Block, $objectClass, $DNSType, $DNSRecord, $DNSTTL) = @_;
  178.  
  179.                 $self->printZone($DNSZoneName);
  180.                 $self->printContainer($DNSHostName, $DNSZoneName);
  181.                 $self->printSOA('@', $DNSZoneName, $self->{DNSPrimaryns}, $self->{DNSAdminEmail}, $self->{DNSSerial}, $self->{DNSRefresh}, $self->{DNSRetry}, $self->{DNSExpire}, $self->{DNSMinimum}, $self->{SOADNSTTL});
  182.  
  183.                 my $Count = $self->{printedRecords}{count}{$DNSZoneName}{$DNSHostName}{$DNSRecord} ++ ;
  184.                 $Count = $Count || '';
  185.  
  186.                 $DNSRecord .= $Count;
  187.  
  188.                 # Some records we want to have 0-n appended regardless as we're more likely to have more then one.
  189.                 if ($DNSType =~ /^(?:mx|ns|rp|txt)$/i && !($DNSRecord =~ /\d+$/)) {
  190.                         $DNSRecord .= '0';
  191.                 }
  192.  
  193.                 print <<EOF;
  194. # $DNSRecord ($DNSType) Record {$DNSHostName.$DNSZoneName.}
  195. dn: DNSRecord=$DNSRecord,DNSHostName=$DNSHostName,DNSZoneName=$DNSZoneName,$self->{basedn}
  196. objectClass: $objectClass
  197. DNSType: $DNSType
  198. DNSRecord: $DNSRecord
  199. DNSHostName: $DNSHostName
  200. DNSTTL: $DNSTTL
  201. $Block
  202.  
  203. EOF
  204.         }
  205.  
  206. package main;
  207.  
  208. my $zone = ZoneParser->new();
  209.  
  210. $zone->{basedn} = 'ou=dns,dc=ibm,dc=com';
  211.  
  212. $zone->parse('ibm.com.hosts');

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.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings