Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

IP troubles

by toadi (Chaplain)
on Dec 18, 2001 at 01:24 UTC ( [id://132675]=perlquestion: print w/replies, xml ) Need Help??

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

I tried several solutions. But is it possible to use Net::Netmask to do this:

(This is just demo code to illustrate my point)

%maskcount = ( "/25" => "128", "/26" => "64", "/27" => "32", "/28" => "16"); # loop POPs while($line=<STDIN>) { chomp($line); next unless ($line=~/^\d/); ($ipbase,$mask)=split(/;/,$line); @ip=split(/\./,$ipbase); $areacode =~ s/^0//; print "$areacode: $ipbase/$mask\n"; # do not load first (=network) and second (=gateway) ip addressess $startip=2; # do not load last (=broadcast) ip address $endip=$maskcount{$mask}-1; print $endip; for ($cnt=$startip;$cnt<$endip;$cnt++) { $ipend[0]=$ip[0]; $ipend[1]=$ip[1]; $ipend[2]=$ip[2]; $ipend[3]=$ip[3]+$cnt; if ($ipend[3]>255) { $ipend[2]=$ip[2]+1; $ipend[3]=$ip[3]-255; } $ipend = join(".",@ipend); print "$ipend\n"; } }
I just want to generate a list of IP's. I know the start ip and the network mask and want to generate the whole list of IP's from that range. I tried this approach, but there , must be a cleaner way to do this...

--
My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: IP troubles
by jwest (Friar) on Dec 18, 2001 at 02:07 UTC
    Does this fit the bill?

    use strict; use warnings; use Net::Netmask; while (<>) { chomp; s/;/:/; my $block = new Net::Netmask($_); if (not $block) { warn "Bad CIDR specification"; next; } my @ip = $block->enumerate(); # Remove the network and gateway addresses. shift(@ip); shift(@ip); # Remove the broadcast address. pop(@ip); for my $ip (@ip) { print $ip, "\n"; } }

    --jwest

    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
      How should the data look as input?

      --
      My opinions may have changed,
      but not the fact that I am right

        Sorry - I should have specified that. As per your example, you can enter it as:

        192.168.1.0;255.255.255.0

        Or, since we're using Net::Netmask:

        192.168.1.0/24
        192.168.1.0:255.255.255.0
        192.168.1 - which assumes a /24 block
        192.168 - which assumes a /16 block
        192 - which assumes a /8 block
        192.168.1/24
        192.168/16
        etc.

        Net::Netmask is particularly smart about parsing these things, so it's a nice tool to leverage for this type of work.

        --jwest

        -><- -><- -><- -><- -><-
        All things are Perfect
            To every last Flaw
            And bound in accord
                 With Eris's Law
         - HBT; The Book of Advice, 1:7
        
Re: IP troubles
by atcroft (Abbot) on Dec 18, 2001 at 11:38 UTC

    Here is my own code (derived from Re: Checking IP's with an IP/mask) to generate the list, without using Net::Netmask. It also seems to work for supernetted blocks through at least a /8 (just finished that test-16,777,216 addresses and code feels fine). Included is my test code for a block (in this case, 192.168.1.32/28). The code also makes allowances for if you want to generate the list with/without the network, gateway, and/or broadcast addresses included.

    (Might could use some clean-up by way of making sure that $nonetwork, $nogateway, and $nobroadcast are only set either 0 or 1.)

    #!/usr/bin/perl -w -- use strict; # # Set to 1 to remove from generated list, 0 to leave in # my $nonetwork = 1; my $nogateway = 1; my $nobroadcast = 1; my $block = '192.168.1.32'; my $cidr = 28; my $machines = 2**(32 - $cidr); my $lip = unpack("N", pack("C4", split(/\D/, $block, 4))); # # Assumes gateway is at beginning, not end, of block # for (my $i = ($nonetwork + $nogateway); $i < ($machines - $nobroadcast); $i++) my $res = $lip + $i; print(join('.', unpack("C4", pack("N", $res))), "\n"); }

    Comments? Questions? Hope this helps.

    Update: Noted in several of the posts afterwards that the input format was discussed. In my code above, I believe significate modification would likely be required to use netmask rather than CIDR notation.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found