Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

It's your choice, but you don't really need a module for this. The simplest way might just be to match the first three numbers, then do a ">= and <=" on the range in the last one.

A slightly more elegant method exploits the fact that IPv4 address are actually 4 byte values -- notice each number ranges from 0-255 (an unsigned, one byte char). If you pack them in order you have a "big endian" (aka. network byte order) number, which is actually how low level networking uses them. Making this easy:

#!/usr/bin/perl -w use strict; sub dot2int { my $address = pop; my @bytes; while ($address =~ /(\d+)/g) { push @bytes,$1 } return unpack('N',pack('C*',@bytes)); } my $first = dot2int('192.168.1.120'); my $last = dot2int('192.168.1.141'); my @testcases = qw( 192.168.1.130 193.168.1.122 255.0.0.27 192.168.1.127 192.168.1.144 192.168.1.139 ); print "$first to $last = ". ($last-$first)." addresses in range\n"; foreach (@testcases) { print $_; my $int = dot2int($_); if ($int >= $first && $int <= $last) { print " spammer!!!\n"; } else { print " OK.\n" } }

The output:

3232235896 to 3232235917 = 21 addresses in range
192.168.1.130 spammer!!!
193.168.1.122 OK.
255.0.0.27 OK.
192.168.1.127 spammer!!!
192.168.1.144 OK.
192.168.1.139 spammer!!!

I would assume there is a module somewhere that includes a function like that, but I can't tell you which one.


In reply to Re: Module for working with IP Addresses and Ranges by halfcountplus
in thread Module for working with IP Addresses and Ranges by ait

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (5)
    As of 2018-07-17 19:38 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (374 votes). Check out past polls.

      Notices?