Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

ip address calculations

by arc_of_descent (Hermit)
on May 06, 2002 at 11:23 UTC ( #164281=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

Is there any module for calculating ip addresses?
Something like:
  • given a start ip and end ip, output all the valid ip addresses in between.
  • given a ip range calculate the netmask, network and broadcast addresses (something like ipcalc).

    I right now need only the first function.
    I searched CPAN and found Net::IPv4Addr, but its not what i want.
    Hell. i even sat down to write the function, but it takes
    an amazing amount of time to output addresses between say -
    And i'm too ashamed to post it here! :)

  • Replies are listed 'Best First'.
    Re: ip address calculations
    by lhoward (Vicar) on May 06, 2002 at 13:30 UTC
      Net::Netmask is what you need. Can do all the IP/network manipulations you want....
    Re: ip address calculations
    by tachyon (Chancellor) on May 06, 2002 at 11:57 UTC

      Socket the inet() functions and pack and unpack can do the first job. This takes under a second to run:

      use Socket; my $begin = ""; my $end = ""; my $ip = unpack N, inet_aton($begin); my $ip_end = unpack N, inet_aton($end); while ( $ip <= $ip_end ) { print inet_ntoa(pack N,$ip), "\n"; $ip++; }

      NetAddr::IP does the rest.




        Yes! Very Interesting

        But i want to neglect ip addresses which are network or broadcast addresses.
        The above code prints out,, etc.
        I really want this functionality for users who are supposed
        to allocate ip addresses from a pool which they have defined
        using start and end ip addresses.
        I guess i'll have to put additional code to neglect
        the network and broadcast addresses.

        Thanx a lot!


    Re: ip address calculations
    by ariels (Curate) on May 06, 2002 at 11:45 UTC

      For your first code, Perl 5.6.1 lets you do this kind of niftiness:

      my ($s,$e) = unpack 'N2', .; printf("%vd\n",pack 'N',$_) for ($s..$e)
      If you use this code, though (complete with whitespace sensitivity that makes Python look like a Good Idea), you might well be shot. Replace the unpacks with a more direct translation of dotted quadruplets into ints, e.g.
      sub ip_to_num { my $ip = shift; my $n; $n = $n*256+$_ for (split /\./,$ip); $n }

      For the second question (and, indeed, for the first), it would be best to post some code, or at the very least some sample input/output.

        my ($s,$e) = unpack 'N2', .; printf("%vd\n",pack 'N',$_) for ($s..$e) __DATA__ Range iterator outside integer range at line 2.

        This will fail on "real" addresses (that exceed integer range) as shown.




    Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    Node Status?
    node history
    Node Type: perlquestion [id://164281]
    Approved by jlk
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (6)
    As of 2021-10-26 09:58 GMT
    Find Nodes?
      Voting Booth?
      My first memorable Perl project was:

      Results (90 votes). Check out past polls.