Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

getting list of IP's from range

by kathys39 (Acolyte)
on May 27, 2009 at 12:04 UTC ( [id://766389]=perlquestion: print w/replies, xml ) Need Help??

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

As output from a client's tool, I get a range of IP's that is similar to "10.0.0.0-50". I need to break this up and get the list of individual IP's. I've tried using NetAddr::IP, but it doesn't like the range I"m feeding it. I can get all the ip's by splitting the range on the hyphen, getting the first 3 octets from the first string, and rebuilding the second string etc, but thought there might be a faster way to do this. This output (10.0.0.0-50) can't be changed - the client uses a their own script and is not willing to modify it. Any idea's on a faster way to do this?

Replies are listed 'Best First'.
Re: getting list of IP's from range
by jettero (Monsignor) on May 27, 2009 at 12:13 UTC

    I think you want to try to get Net::Netmask to work. You may need to re-write 10.0.0.0-50 slightly (to 10.0.0.0-10.0.0.50) but it'll otherwise probably do this job -- and it may do it anyway.

    -Paul

Re: getting list of IP's from range
by targetsmart (Curate) on May 27, 2009 at 12:17 UTC
    your idea of splitting and using the range operator is quite faster and perl-ish way of doing.
    however can you pass that many IPs(after elaborating the range) to the client script, will it handle that many arguments properly?.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: getting list of IP's from range
by ikegami (Patriarch) on May 27, 2009 at 13:32 UTC

    but thought there might be a faster way to do this.

    It's pretty trivial:

    if ( $start =~ /^(.*\.)(\d+)-(\d+)/ ) { $start = "$1$2"; $end = "$1$3"; } else { $end = $start; }
Re: getting list of IP's from range
by FloydATC (Deacon) on May 27, 2009 at 13:27 UTC
    This code doesn't interpolate but it parses your input into something NetAddr::IP might agree with...:
    my $range = shift @ARGV; # E.g. 10.0.10-20.0 if ($range =~ /([0-9\.]+)\-([0-9\.]+)/) { my $begin = $1; my $end = $2; my @begin_quad = split(/\./, $begin); my @end_quad = split(/\./, $end); push @begin_quad, 0 while @begin_quad < 4; my $index = 0; while (@end_quad < 4) { unshift @end_quad, $begin_quad[$index]; $index; } print "Begin: ".join('.', @begin_quad)."\n"; print "End: ".join('.', @end_quad)."\n"; } else { die "Expected a range"; } exit;
    -- Time flies when you don't know what you're doing
Re: getting list of IP's from range
by poolpi (Hermit) on May 27, 2009 at 15:14 UTC
Re: getting list of IP's from range
by FloydATC (Deacon) on May 27, 2009 at 13:10 UTC
    Will the part after the hyphen always be a single octet, or do you also have to expect e.g. 10.0.0.0-50.0 or 10.0.0.0-10.50.0.0 ?

    -- Time flies when you don't know what you're doing

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-29 11:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found