http://qs1969.pair.com?node_id=252252

!unlike has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I've just knocked together a quick script to check the reverse DNS entries for a /16. It's purpose is to print any IP addres that doesn't have a reverse entry (currently I'm not worried about forward and reverse entries being the same).

I've written the following, but would be interested in a "better" way of doing this:

#!/usr/bin/perl use Net::DNS $res = Net::DNS::Resolver->new; for($c = 0; $c <= 255; $c++) { for($d = 0; $d <= 255; $d++) { $query= $res->query("81.86.$c.$d", "PTR"); print "81.86.$c.$d" unless $query; } }

Anyone who fancies a quick blast at Perl golf, but all means put on you spikey shoes and silly clothes and swing away.

!unlike

I write my Perl code like how I like my sex: fast and dirty. ;)

edited: Tue Apr 22 14:18:43 2003 by jeffa - title change (was: Seeking a better way)

Replies are listed 'Best First'.
Re: Better way to check reverse DNS entries
by rob_au (Abbot) on Apr 22, 2003 at 13:03 UTC
    It was in reply to a similar post to this that I was motivated to write the snippet I posted at Iterate network hosts - This subroutine of code uses the Net::Netmask module to iterate through network hosts specified by network address and subnet mask. The network address and subnet mask passed to this subroutine can take the form of separate network block and subnet masks (eg. 192.168.1.0 and 255.255.255.0) or CIDR notation (eg. 192.168.1.0/24).

    For example:

    #!/usr/bin/perl use Net::DNS use Net::Netmask; my $resolver = Net::DNS::Resolver->new; print $_, "\n" for grep { ! $resolver->query( $_, 'PTR' ) } ips( '81.86/16' ); sub ips { my $net = Net::Netmask->new(@_); wantarray ? $net->enumerate : \@{$net->enumerate}; }

    Never should your network administration scripts be non-portable!

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001001001110"))'

Re: Better way to check reverse DNS entries
by Abigail-II (Bishop) on Apr 22, 2003 at 13:07 UTC
    It's only a small piece of code, and if it works for you, why bother? I find all questions of the form "can this be done 'better'", or "what is the 'best' module to use when doing X" awkward. It all depends on your definition of "best". Some people say "best" means, "fastest", or "uses the least memory". For both types, the answer would usually be "do it in C instead of Perl". If with best you mean "least amount of key strokes", you should direct your question to a mailinglist about golf.

    All I can say is how I would code it. Whether that is "better" or not, I leave to you. Afterall, it's your code, you get to decide what is better.

    #!/usr/bin/perl use strict; use warnings; use Net::DNS; my $res = Net::DNS::Resolver -> new; my $net = "81.86"; foreach my $c (0x00 .. 0xFF) { foreach my $d (0x00 .. 0xFF) { my $ip = "$net.$c.$d"; print $ip unless $res -> query ($ip => 'PTR') } } __END__

    I used 0x00 .. 0xFF instead of 0 .. 255 purely for aesthetic values.

    Abigail

      Thanks Abigail, et al,
      I suppose by "better" I mean different. I know I have a certain mentality when it come to writing code. It usually works well for me, but every now and again I have difficulties in solving a problem this way.

      The code submitted worked, but was a simple enough problem that solutions provided by others would hopefully help me see another way of thinking.

      Just looking to improve I guess.

      !unlike

      I write my Perl code like how I like my sex: fast and dirty. ;)

Re: Better way to check reverse DNS entries
by ctilmes (Vicar) on Apr 22, 2003 at 12:57 UTC
    #!/usr/bin/perl use Net::DNS $res = Net::DNS::Resolver->new; for($c = 0; $c <= 255; $c++) { for($d = 0; $d <= 255; $d++) { print "81.86.$c.$d" unless $res->query("81.86.$c.$d", "PTR"); } }
      foreach $c (0..255) { foreach $d (0..255) { print "81.86.$c.$d" unless $res->query("81.86.$c.$d", "PTR"); } }
      Here's something a bit different:
      #!/usr/bin/perl -l use strict; use Net::Netmask; use Socket; gethostbyaddr(inet_aton($_), AF_INET) or print for Net::Netmask->new("81.86.0.0/16")->enumerate();

      -Matt