looping through IP address range

Discussion in 'Perl Misc' started by justme, Jul 7, 2004.

  1. justme

    justme Guest

    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
     
    justme, Jul 7, 2004
    #1
    1. Advertising

  2. 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
    }
     
    Eugene Mikheyev, Jul 7, 2004
    #2
    1. Advertising

  3. 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
     
    Josef Moellers, Jul 7, 2004
    #3
  4. justme

    Anno Siegel Guest

    justme <> 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
     
    Anno Siegel, Jul 7, 2004
    #4
  5. > for my $ip ( map "10.10.10.$_", 0 .. 255 ) {
    > # ...
    > }

    Interpolation takes time, joining is faster...
     
    Eugene Mikheyev, Jul 7, 2004
    #5
  6. justme

    Anno Siegel Guest

    Eugene Mikheyev <> 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
     
    Anno Siegel, Jul 7, 2004
    #6
  7. 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.
     
    Eugene Mikheyev, Jul 7, 2004
    #7
  8. Purl Gurl <> 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;
     
    Peter J. Acklam, Jul 7, 2004
    #8
  9. Purl Gurl <> 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
    ("" 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;
     
    Peter J. Acklam, Jul 7, 2004
    #9
  10. Peter J. Acklam <> wrote:
    > Purl Gurl <> 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\
     
    Jonathan Stowe, Jul 14, 2004
    #10
  11. Purl Gurl wrote:
    > Jonathan Stowe wrote:
    >
    >
    >>Peter J. Acklam wrote:
    >>
    >>>Purl Gurl 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.

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

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jul 15, 2004
    #11
  12. Purl Gurl wrote:
    > Jonathan Stowe wrote:
    >
    >
    >>Peter J. Acklam wrote:
    >>
    >>>Purl Gurl 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.

    >
    >
    > 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.
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Moellers, Jul 15, 2004
    #12
  13. Josef Moellers <> wrote:
    > Purl Gurl wrote:
    >> Jonathan Stowe wrote:
    >>
    >>
    >>>Peter J. Acklam wrote:
    >>>
    >>>>Purl Gurl 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.

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


    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\
     
    Jonathan Stowe, Jul 15, 2004
    #13
  14. justme

    Joe Smith Guest

    Purl Gurl wrote:

    > 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
     
    Joe Smith, Jul 16, 2004
    #14
  15. justme

    Matt Garrish Guest

    "Purl Gurl" <> wrote in message
    news:...
    > Jonathan Stowe wrote:
    >
    > > 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).

    >
    > 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
     
    Matt Garrish, Jul 17, 2004
    #15
  16. Joe Smith <> wrote:
    > Purl Gurl wrote:
    >
    >> 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:


    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\
     
    Jonathan Stowe, Jul 19, 2004
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    996
    Antoon Pardon
    Jul 25, 2006
  2. Lambda
    Replies:
    2
    Views:
    424
    James Kanze
    Jul 16, 2008
  3. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    169
    Tomoyuki Kosimizu
    Nov 27, 2003
  4. Aaron
    Replies:
    2
    Views:
    574
    dhtml
    Apr 10, 2011
  5. Replies:
    5
    Views:
    302
Loading...

Share This Page