Hey everyone, just wanted to post some code to have it reviewed.




#!/usr/bin/perl use Net::DNS::Resolver; use NetAddr::IP; use vars qw/ %opt /; ###################################################################### +################### # # # Sub to perform DNS lookup # # Too lazy to write one sub with var for fw/rev so did two instead + # # # ###################################################################### +################### sub revlookup { my $res=Net::DNS::Resolver->new; $res->nameservers($server); my $search = $res->search($input); if ($search) { foreach $rr ( $search->answer) { my $type=$rr->type; if ($type eq "A") { $host=$rr->address; } if ($type eq "PTR") { $host=$rr->ptrdname; } else { print "$input\t$rr->type\n"; } #print "$input\t$host\n"; push(@reverseip,$input); push (@reversename, $host); } } } sub fwlookup { my $res=Net::DNS::Resolver->new; $res->nameservers($server); my $search = $res->search($input); if ($search) { foreach $rr ( $search->answer) { my $type=$rr->type; if ($type eq "A") { $host=$rr->address; } if ($type eq "PTR") { $host=$rr->ptrdname; } if ($type eq "CNAME") { $host=$rr->cname; } else { #print "$input\t$rr->type\n"; } #print "$input\t$host\n"; push(@forwardip,$host); push (@forwardname, $input); } } else { push (@forwardip, $res->errorstring); push (@forwardname, $input); } } ###################################################################### +################### # # # sub to check command line options passed to program for validity + # # # ###################################################################### +################### sub options { use Getopt::Long; if ($#ARGV lt 0) { &usage; } GetOptions ("r:s" => \$cidr, "h" => \$help, "s:s" => \$server); &usage if $help; &usage if not $cidr; &usage if not $server; } ###################################################################### +################### # # # sub to display a usage message # # # ###################################################################### +################### sub usage { print "-h help message\n"; print "-r [range] to search in CIDR format: 128.0/8\n"; print "-s [server] to direct queries to\n"; exit 1; } ###################################################################### +################### # # # Main program # # Too lazy to write sub to do check so just shoved it in here + # # # ###################################################################### +################### &options; my $ip = new NetAddr::IP($cidr); $range = $ip->range(); $bcast = $ip->broadcast(); print "Searching range: $range: Broadcast $bcast\n"; while ($ip < $ip->broadcast) { ($iponly,$mask) = split /\//, $ip; $input=$iponly; &revlookup; $ip++; } foreach (@reversename) { $input=$_; &fwlookup; } for ($count=0; $count ne $#reversename; $count++) { $revip=$reverseip[$count]; $revname=$reversename[$count]; $fwip=$forwardip[$count]; $fwname=$forwardname[$count]; if ($revip ne $fwip) { print "\n\n"; print "REVERSE: $revip\t$revname\n"; print "FORWARD: $fwname\t$fwip\n"; } if ($fwname ne $revname) { print "\n\n"; print "WARNING: $revname\t$fwname\n"; } }


I know it's not the best in the world, but it sort of works.

My question is: would it be better to have a variable passing to a lookup sub to say "this is forward" or "this is reverse"
I know that there is unnecessary use of too many arrays here



Anyway, any comments appreciated.




Thanks for the feedback everyone.I'll probably post another updated version in a week or so when I get time to update it.....
comments much appreciated.

In reply to code review by svankalken

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.