Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

ip range for config files

by smackdab (Pilgrim)
on Nov 05, 2003 at 05:38 UTC ( [id://304631]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All, I was looking for a somewhat standard solution to my need to enhance my hosts list. I would like to support: hosts, ipaddr, ipaddr range, and I think CIDR.

I never knew what the "/xxx" meant when I saw it, still don't know anything about subnets, other than the network folks might want to use then instead of ip address ranges...

I have been looking at NetAddr::IP and I think it will work. I did find that if you specify some IP addresses, the CPU will peg 99% and take 500Megs+ of ram (before ctrl+c) YIKES This might not be safe to use...(my guess is that I specified an invalid range, but since it could happen by accident, I don't want my program to take all the memory, I'd rather it crashed !

What does everyone else use?

Here is the test program I saw...top $hosts line takes TONS of ram and bottom line works

I am open to any suggestions on the format to use
use NetAddr::IP; #$hosts= '172.21.3.128-172.21.3.140;10.1.1.1;192.168.0.1/8'; #$hosts= '172.21.3.128-172.21.3.140;10.1.1.1;10.0.0.0/30'; push @hosts, split ';', $hosts; for my $cidr( @hosts ) { print "$cidr\n"; my $n = NetAddr::IP->new( $cidr ); for my $ip( @{$n->hostenumref} ) { print "\t", $ip->addr, "\n"; } }

Replies are listed 'Best First'.
Re: ip range for config files
by zengargoyle (Deacon) on Nov 05, 2003 at 07:55 UTC

    try it without 192.168.0.1/8, it's invalid and a /8 is huge (2**24).

    which is your second problem, in your for loop your forcing all of the individual IPs to be created at once which takes tons of memory and time. you want to read the documentation and use the iterator.

    i'm too lazy to look it up at the moment, but it'll be something like:

    while ( my $ip = $n->next ) { # do something with ip }

    this will only create one IP at a time and be much much faster and use much less memory.

    192.168.0.1/8 is invalid. 192.0.0.0/8 would be valid. unless your trying to specify a host and a network at the same time (as on a network interface).

    all in all i wouldn't worry about using 192.168.0.1-12 format. i've never seen a need in many years of keeping lists of IPs. most people who have to do whatever with a lot of IPs that can't be constrained into CIDR notation will already have a list with each IP for use with other programs that take IP addresses.

Re: ip range for config files
by Roger (Parson) on Nov 05, 2003 at 05:46 UTC
    There is currently an active discussion thread on CIDR: Iterating through all IP addresses in a CIDR, node 304414. I think it might have what you are looking for.

      Thanks, I saw that thread during a search and just assumed it was OLD. But I am finding that NetAddr::IP doesn't seem to support ranges, it just looked like it. I might drop CIDR all together...
Re: ip range for config files
by ehdonhon (Curate) on Nov 05, 2003 at 05:58 UTC

    I tend to prefer Net::Netmask myself. You can achieve similar code to your test code above using Netmask's enumerate() method.

      Thanks, after looking at Net::Netmask, it doesn't appear that it will accept an IP range:
      192.168.0.1 - 192.168.0.100 and have 100 IP address returned...am I right? I also don't think NetAddr::IP will also. I think the formats are not compatible...

        The thing about 192.168.0.1 - 192.168.0.100 is that it is impossible to represent that exact range in one net block. You have to use several netblocks of varying sizes to do it. So no, Net::Netmask will not let you do it all in one block because its impossible.

        It does, however, give you the range2cidrlist method that will break that range into a series of valid netblocks for you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found