Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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 $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= ';;'; #$hosts= ';;'; 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, 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. is invalid. 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 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: - and have 100 IP address I right? I also don't think NetAddr::IP will also. I think the formats are not compatible...

        The thing about - 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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-07-24 16:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.