Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > looping through IP address range

Reply
Thread Tools

looping through IP address range

 
 
justme
Guest
Posts: n/a
 
      07-07-2004
hi

i wanted to loop through an IP address range, something like

my @range = '10.10.10.0 10.10.10.255';
foreach my $ip ( @range )
{
..do something
}



how can i do that in perl ? thanks
 
Reply With Quote
 
 
 
 
Eugene Mikheyev
Guest
Posts: n/a
 
      07-07-2004
You may do something like
my @ip_parts = (10, 10, 10);
for (my $last_part = 0; $last_part <= 255; $last_part++)
{
my $ip = join '.', @ip_parts, $last_part;
# ... whatever
}


 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      07-07-2004
justme wrote:
> hi
>
> i wanted to loop through an IP address range, something like
>
> my @range = '10.10.10.0 10.10.10.255';
> foreach my $ip ( @range )
> {
> ..do something
> }
>
>
>
> how can i do that in perl ? thanks


I'd convert between DDN and (unsigned) integers back and forth:

int->ddn sprintf("%vd",pack("N", $n))

ddn->int unpack("N", inet_aton($ddn))

You'll need to use Socket for inet_aton (untested code follows):

use Socket;
my @range = ('10.10.10.0', '10.10.10.255');

foreach my $ip (unpack("N", inet_aton($range[0])) .. unpack("N",
inet_aton($range[1])))
{
$ip = sprintf("%vd",pack("N", $ip));
..do something
}

HTH,

Josef
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-07-2004
justme <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> hi
>
> i wanted to loop through an IP address range, something like
>
> my @range = '10.10.10.0 10.10.10.255';
> foreach my $ip ( @range )
> {
> ..do something
> }


for my $ip ( map "10.10.10.$_", 0 .. 255 ) {
# ...
}

Anno
 
Reply With Quote
 
Eugene Mikheyev
Guest
Posts: n/a
 
      07-07-2004
> for my $ip ( map "10.10.10.$_", 0 .. 255 ) {
> # ...
> }

Interpolation takes time, joining is faster...


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-07-2004
Eugene Mikheyev <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > for my $ip ( map "10.10.10.$_", 0 .. 255 ) {
> > # ...
> > }

> Interpolation takes time, joining is faster...


....and interpolation is more readable. A matter of priorities.

Anno
 
Reply With Quote
 
Eugene Mikheyev
Guest
Posts: n/a
 
      07-07-2004
use strict;
use Benchmark;

timethese(
10000,
{
Join1 => sub {
my @parts = (10, 10, 10);
for my $last_part (0..255) {
my $ip = join '.', @parts, $last_part;
do_something( $ip );
}
},
Join2 => sub {
my @parts = (10, 10, 10);
for (my $last_part = 0; $last_part <= 255; $last_part++) {
do_something(join '.', @parts, $last_part);
}
},
Join3 => sub {
for my $last_part (0 .. 255) {
do_something("10.10.10.$last_part");
}
},
Exp1 => sub {
for my $ip ( map "10.10.10.$_", 0 .. 255 ) {
do_something( $ip );
}
},
Exp2 => sub {
map do_something("10.10.10.$_"), 0 .. 255
}
}
);

sub do_something {
my $myvar = shift;
}


Results:
------------
Benchmark: timing 10000 iterations of Exp1, Exp2, Join1, Join2, Join3...
Exp1: 5 wallclock secs ( 5.17 usr + 0.00 sys = 5.17 CPU) @
1933.49/s (n=10000)
Exp2: 5 wallclock secs ( 4.75 usr + 0.00 sys = 4.75 CPU) @
2105.26/s (n=10000)
Join1: 6 wallclock secs ( 5.19 usr + 0.00 sys = 5.19 CPU) @
1927.53/s (n=10000)
Join2: 4 wallclock secs ( 4.28 usr + 0.00 sys = 4.28 CPU) @
2335.90/s (n=10000)
Join3: 3 wallclock secs ( 2.88 usr + 0.00 sys = 2.88 CPU) @
3478.26/s (n=10000)

Join3 results show that interpolating and then looping is worse than just
looping and interpolating
I bet that's because of foreach cycle.
Strange why Exp2 is worse than Join3... But maybe that's because map needs
to create a list for output.


 
Reply With Quote
 
Peter J. Acklam
Guest
Posts: n/a
 
      07-07-2004
Purl Gurl <(E-Mail Removed)> wrote:

> justme wrote:
>
> > i wanted to loop through an IP address range, something like
> >
> > my @range = '10.10.10.0 10.10.10.255';
> > foreach my $ip ( @range )

>
> Others have commented on use of (0 .. 255) syntax.
>
> You should not manipulate nor use .0 and .255 in your syntax.
> Those are CIDR / netmask broadcast addresses not to be
> manipulated.


That's just stupid to say as long as you have no idea what OP is
going to do with the IP addresses.

Peter

--
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((??:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
 
Reply With Quote
 
Peter J. Acklam
Guest
Posts: n/a
 
      07-07-2004
Purl Gurl <(E-Mail Removed)> wrote:

> Peter J. Acklam wrote:
>
> > Purl Gurl wrote:

>
> You have forgotten you have me killfiled.


No, I said that you are in my "score file", not "kill file". In
my case it implies that messages from you are automatically marked
as read and displayed in a less significant color and font.

Actually, here is an excerpt from my ~/News/perl.SCORE file:

;; Cool people
("Larry Wall" 800 nil s)
("tchrist@.*perl\\.com" 500 nil r) ; Tom Christiansen
("(E-Mail Removed)" 200 nil s) ; Mark-Jason Dominus
...

;; Stupid people
("Purl Gurl" -1000 nil S)
("Kira" -1000 nil S) ; aka "Purl Gurl"
("Godzilla" -1000 nil S) ; ditto
(".*@purlgurl\\.net" -1000 nil r) ; ditto
...

Peter

--
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((??:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
 
Reply With Quote
 
Jonathan Stowe
Guest
Posts: n/a
 
      07-14-2004
Peter J. Acklam <(E-Mail Removed)> wrote:
> Purl Gurl <(E-Mail Removed)> wrote:
>
>> justme wrote:
>>
>> > i wanted to loop through an IP address range, something like
>> >
>> > my @range = '10.10.10.0 10.10.10.255';
>> > foreach my $ip ( @range )

>>
>> Others have commented on use of (0 .. 255) syntax.
>>
>> You should not manipulate nor use .0 and .255 in your syntax.
>> Those are CIDR / netmask broadcast addresses not to be
>> manipulated.

>
> That's just stupid to say as long as you have no idea what OP is
> going to do with the IP addresses.
>


More to the point any of the addresses can be network and/or broadcast
addresses - it's been a long time since we had simply class A, B and C
networks.

/J\
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
looping through json array loops through the characters instead ofthe values Aaron Javascript 2 04-10-2011 05:58 PM
Looping through dataset outputting HTML radiobuttons Robert H ASP .Net 0 12-04-2003 11:02 PM
Looping through rows in a DataGrid Diva ASP .Net 1 07-24-2003 06:10 PM
Looping through all the form fields in the code behind ? Aemca ASP .Net 1 07-22-2003 08:31 AM



Advertisments