svankalken has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: code review
by chromatic (Archbishop) on Jul 08, 2004 at 00:03 UTC | |
|
Re: code review
by gaal (Parson) on Jul 08, 2004 at 04:55 UTC | |
|
Re: code review
by William G. Davis (Friar) on Jul 08, 2004 at 09:45 UTC | |
by Madams (Pilgrim) on Jul 12, 2004 at 00:06 UTC |