looping through IP address range

J

justme

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
 
E

Eugene Mikheyev

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
}
 
J

Josef Moellers

justme said:
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
 
A

Anno Siegel

justme said:
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
 
E

Eugene Mikheyev

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.
 
P

Peter J. Acklam

Purl Gurl said:
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
 
P

Peter J. Acklam

Purl Gurl said:
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 address 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
 
J

Jonathan Stowe

Peter J. Acklam said:
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\
 
J

Josef Moellers

Purl said:
Jonathan Stowe wrote:




Really?

Try setting your router ip address to end with .0 or
to end with .255 if you believe your words.

In a class B network, why not?
That's what Jonathan says.
 
J

Josef Moellers

Purl said:
Jonathan Stowe wrote:




Really?

Try setting your router ip address to end with .0 or
to end with .255 if you believe your words.

In a class B network: why not?
More general and more to Jonathan's point: in a network with less that
24 bits in the network address (e.g. when the netmask is 255.255.254.0)
a valid subnet address may end with the bits 100000000 or 011111111!
The only restriction is that a subnet address cannot be all 0's or all 1's.
 
J

Jonathan Stowe

Josef Moellers said:
In a class B network: why not?
More general and more to Jonathan's point: in a network with less that
24 bits in the network address (e.g. when the netmask is 255.255.254.0)
a valid subnet address may end with the bits 100000000 or 011111111!
The only restriction is that a subnet address cannot be all 0's or all 1's.

Precisely, and going in the other direction where you have, say, a
netmask of 255.255.255.252 every fourth address would be a subnet
address. Proscribing the use of any address is pointless unless we know
what the OPs intentions are (which we apparently don't).

/J\
 
J

Joe Smith

Purl said:
Really?

Try setting your router ip address to end with .0 or
to end with .255 if you believe your words.

Works just fine if you are not using a /24 subnet.

For the netmask of 255.255.240.0 for a /20 subnet:
192.168.00.000 = address of the network itself
192.168.00.001 = lowest usable IP address
192.168.15.254 = highest usable IP address
192.168.15.255 = broadcast address

192.168.01.000 = legal address
192.168.01.255 = legal address
192.168.02.000 = legal address
192.168.02.255 = legal address
192.168.03.000 = legal address
192.168.03.255 = legal address
192.168.04.000 = legal address
192.168.04.255 = legal address
etc.
Try setting your LAN card ip address to end with .0 or
to end with .255 if you believe your words.

Works just fine if not using 255.255.255.0 as the netmask.
Try setting a port forwarding ip address to end with .0 or
to end with .255 if you believe your words.

Works just fine if not using 255.255.255.0 as the netmask.
Try setting your firewall ip address to end with .0 or
to end with .255 if you believe your words.

Works just fine if not using 255.255.255.0 as the netmask.
Try binding your webserver ip address to end with .0 or
to end with .255 if you believe your words.

Works just fine if not using 255.255.255.0 as the netmask.
So run some tests then report back to readers about
how true are your words.

Works just fine if not using 255.255.255.0 as the netmask.

-Joe
 
M

Matt Garrish

Purl Gurl said:
Readers continue to await a direct response from you
regarding my examples. Although I am aware participants
here make use of avoidance on a regular basis, as you
are doing, I am not one of those participants and do
expect you to directly address my comments.

Ha! I think you've topped yourself with that idiotic comment. You've never
yet given a direct response to any of the too-numerous-to-count times you've
been proven wrong. Why don't you tell everyone which platform POSIX is
broken on, as Dave Cross has been trying to get out of you? Imagine my
surprise when you edit that question out of any response you might have...

Matt
 
J

Jonathan Stowe

Joe Smith said:
Works just fine if you are not using a /24 subnet.

For the netmask of 255.255.240.0 for a /20 subnet:

Or indeed a /8 :

[root@orpheus devel]# ifconfig vmnet8
vmnet8 Link encap:Ethernet HWaddr 00:50:56:C0:00:08
inet addr:10.1.0.0 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

[root@orpheus devel]# ping 10.1.0.0
PING 10.1.0.0 (10.1.0.0) 56(84) bytes of data.
64 bytes from 10.1.0.0: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 10.1.0.0: icmp_seq=2 ttl=64 time=0.016 ms

--- 10.1.0.0 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.016/0.019/0.023/0.005 ms
[root@orpheus devel]# telnet 10.1.0.0 80
Trying 10.1.0.0...
Connected to 10.1.0.0 (10.1.0.0).


And:

[root@orpheus devel]# ifconfig vmnet1
vmnet1 Link encap:Ethernet HWaddr 00:50:56:C0:00:01
inet addr:10.1.0.255 Bcast:10.255.255.255 Mask:255.0.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:45 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

[root@orpheus devel]# ping 10.1.0.255
PING 10.1.0.255 (10.1.0.255) 56(84) bytes of data.
64 bytes from 10.1.0.255: icmp_seq=1 ttl=64 time=0.025 ms
64 bytes from 10.1.0.255: icmp_seq=2 ttl=64 time=0.020 ms

--- 10.1.0.255 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.020/0.022/0.025/0.005 ms
[root@orpheus devel]# telnet 10.1.0.255 80
Trying 10.1.0.255...
Connected to 10.1.0.255 (10.1.0.255).


I'm not quite sure what else I should have expected.

/J\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top