I am working on a script that compares a local zonefile to the results of gethostby*() and writes to an error file if the two do not match. I have all of the SOA, A, CNAME records completed. The MX records are giving me trouble in doing the compare though.

Here is the code for the MX part

#!/usr/bin/env perl -w use Net::DNS; use DNS::ZoneParse; use Socket; $res = Net::DNS::Resolver->new(); my $ZONEPATH = $ARGV[0] or die "You Must specify the /full/path/to/z +onefiles/domain.com\n"; my @fields = split(/\//,$ARGV[0]); $DOMAIN = $fields[-1]; my $data_file = "/tmp/$DOMAIN.err"; open FILE, "+>$data_file" or die "can't open $data_file $!"; my $zonefile = DNS::ZoneParse->new($ZONEPATH); my $mx_records = $zonefile->mx(); # DNS query MX records my @mx = mx($res, $DOMAIN); my $DNS_mx_records_size = scalar @mx; my $mx_records_size = scalar @$mx_records; my $mx_priority = $mx_record->{priority}; my $mx_hostname = $mx_record->{host}; if ($DNS_mx_records_size ne $mx_records_size) { print FILE "$DOMAIN:MX:Count:$mx_records_size//Count:$DNS_mx_r +ecords_size\n"; } foreach $mx_record (@$mx_records) { print FILE $DOMAIN, ":MX:", $mx_record->{priority}, ":", $mx_record- +>{host},"//\n"; } foreach my $record (@mx) { print FILE "DNS:MX:", $record->preference, ":", $record->exchang +e, "\n"; } close (FILE);

Here is an example of the A rr comparison:

my $a_records = $zonefile->a(); # A records foreach my $record (@$a_records) { my $a_name = $record->{name}; $a_name =~ s/$/.$DOMAIN/g; $a_name =~ s/\@.//g; my $a_host = $record->{host}; my $a_name_packed_ip = gethostbyname("$a_name.$DOMAIN"); if (defined $a_name_packed_ip) { $a_name_ip_address = inet_ntoa($a_name_packed_ip); } my $a_host_packed_ip = gethostbyname("$a_host"); if (defined $a_host_packed_ip) { $a_host_ip_address = inet_ntoa($a_host_packed_ip); } if ($a_name_ip_address ne $a_host_ip_address) { print FILE "$DOMAIN:A:$a_name:$a_name_ip_address//DNS: +A:$a_host_ip_address\n"; } }

I am not having any luck comparing the MX records from the zonefile to the MX records returned in the lookup. Ive banged my head against this one for a while, so Now I turn to the brethren perl monks of the world for advice

I think it may have something to do with the fact that I have an array in mx_records and a hash in mx.
What I cant get my noodle around is that if this zonefile is a MAJOR change I may see 1 MX rr in the query and have 4 or 5 in the zonefile.

ANY Help is appreciated
Darrin

OOPS... I guess I should post an example of the output:

SOMEDOMAIN.com:SOA:Serial:200912070901//DNS:SOA:Serial107082511 SOMEDOMAIN.com:SOA:Primary:ns.SOMEZONE.com.//DNS:SOA:Primary:ns.SOMEBI +GMX.com SOMEDOMAIN.com:SOA:Refresh:3600//DNS:SOA:Refresh:10800 SOMEDOMAIN.com:SOA:TTL:3600//DNS:SOA:TTL:7200 SOMEDOMAIN.com:SOA:eMail:postmaster.SOMEOTHMAIL.com.//DNS:SOA:eMail:na +mehost.SOMEBIGMX.com SOMEDOMAIN.com:SOA:Expire:3600000//DNS:SOA:Expire:604800 SOMEDOMAIN.com:A:SOMEDOMAIN.com:111.111.111.11//DNS:A:222.222.22.222 SOMEDOMAIN.com:A:ftp.SOMEDOMAIN.com:111.111.111.12//DNS:A:222.222.22.2 +22 SOMEDOMAIN.com:A:mail.SOMEDOMAIN.com:111.111.111.25//DNS:A:11.22.123.2 +34 SOMEDOMAIN.com:A:pop.SOMEDOMAIN.com:111.111.111.35//DNS:A:11.22.123.23 +4 SOMEDOMAIN.com:A:smtp.SOMEDOMAIN.com:111.111.111.35//DNS:A:11.22.123.2 +34 SOMEDOMAIN.com:A:webmail.SOMEDOMAIN.com:111.111.111.45//DNS:A:11.22.12 +3.233 SOMEDOMAIN.com:A:www.SOMEDOMAIN.com:111.111.111.11//DNS:A:222.222.22.2 +22 SOMEDOMAIN.com:A:personalmail.SOMEDOMAIN.com:111.111.111.55//DNS:A:11. +22.123.234 SOMEDOMAIN.com:CNAME:imap:111.111.111.11//DNS:CNAME:imap.IMAPMAILSVCS. +net.:222.22.232.22 SOMEDOMAIN.com:CNAME:intermanager:111.111.111.65//DNS:CNAME:intermanag +er.SOMEOTHMAIL.com.:222.22.232.22 SOMEDOMAIN.com:CNAME:selfcare:111.111.111.75//DNS:CNAME:selfcare.SOMEO +THMAIL.com.:222.22.232.22 SOMEDOMAIN.com:MX:Count:4//Count:1 SOMEDOMAIN.com:MX:10:mx1.MXHOSTING.com.// SOMEDOMAIN.com:MX:100:mx2.MXHOSTING.com.// SOMEDOMAIN.com:MX:110:mx3.MXHOSTING.com.// SOMEDOMAIN.com:MX:120:mx4.MXHOSTING.com.// DNS:MX:10:inbound.SOMEDOMAIN.com.SOMEMAIL.net


In reply to NET::DNS and Perl Comparison (hash ne array); by zuzu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.