Professional Web Applications Themes

Enumerating list of IP Addresses from a given range - PERL Beginners

given a start and finish address like this $start_address = "10.11.1.14:; $finish_address = "12.13.2.3"; I need to output generarate a a sequential list of addresses like the following: 10.11.1.14 10.11.1.15 10.11.1.16 .... 12.13.1.252 12.13.1.253 12.13.1.254 12.13.2.1 13.13.2.2 12.13.2.3 I have been experimenting using nested while loops for each octet. The following code works for properly enumerating the first and second octets, but I have not been successful in making the third or forth octets calculate in I continue nesting the loops.Any suggestions? I have looked at nested loops until my eyes are dizzy :-) Surely there has to be a ...

  1. #1

    Default Enumerating list of IP Addresses from a given range

    given a start and finish address like this

    $start_address = "10.11.1.14:;
    $finish_address = "12.13.2.3";

    I need to output generarate a a sequential list of addresses like the
    following:

    10.11.1.14
    10.11.1.15
    10.11.1.16
    ....
    12.13.1.252
    12.13.1.253
    12.13.1.254
    12.13.2.1
    13.13.2.2
    12.13.2.3

    I have been experimenting using nested while loops for each octet. The
    following code works for properly enumerating the first and second
    octets, but I have not been successful in making the third or forth
    octets calculate in I continue nesting the loops.Any suggestions? I
    have looked at nested loops until my eyes are dizzy :-) Surely there has
    to be a better way than this?

    $start_a = "164";
    $start_b = "100";
    $start_c = "10";
    $start_d = "1";

    $finish_a = "164";
    $finish_b = "100";
    $finish_c = "15";
    $finish_d = "254";

    $counter_b = "255";

    #OCTET A
    while ($start_a ne ($finish_a+1))
    {
    #OCTET B
    $counter_b = ($finish_b+1) unless ($start_a ne $finish_a);
    while ($start_b ne $counter_b)
    {
    #build A AND B addresses and output
    $target_addr = $start_a.".".$start_b;
    print "$target_addr\n";
    $start_b++;
    }
    $start_a++;
    $start_b="1";
    $counter_b="255";
    }

    Thanks,

    Brian Burns
    Dizzy Perl Beginner

    Brian Burns Guest

  2. #2

    Default Re: Enumerating list of IP Addresses from a given range

    Brian Burns wrote:
    >
    > given a start and finish address like this
    >
    > $start_address = "10.11.1.14:;
    > $finish_address = "12.13.2.3";
    >
    > I need to output generarate a a sequential list of addresses like the
    > following:
    >
    > 10.11.1.14
    > 10.11.1.15
    > 10.11.1.16
    > ...
    > 12.13.1.252
    > 12.13.1.253
    > 12.13.1.254
    > 12.13.2.1
    > 13.13.2.2
    > 12.13.2.3
    >
    > I have been experimenting using nested while loops for each octet. The
    > following code works for properly enumerating the first and second
    > octets, but I have not been successful in making the third or forth
    > octets calculate in I continue nesting the loops.Any suggestions? I
    > have looked at nested loops until my eyes are dizzy :-) Surely there has
    > to be a better way than this?
    >
    > $start_a = "164";
    > $start_b = "100";
    > $start_c = "10";
    > $start_d = "1";
    >
    > $finish_a = "164";
    > $finish_b = "100";
    > $finish_c = "15";
    > $finish_d = "254";
    >
    > $counter_b = "255";
    >
    > #OCTET A
    > while ($start_a ne ($finish_a+1))
    > {
    > #OCTET B
    > $counter_b = ($finish_b+1) unless ($start_a ne $finish_a);
    > while ($start_b ne $counter_b)
    > {
    > #build A AND B addresses and output
    > $target_addr = $start_a.".".$start_b;
    > print "$target_addr\n";
    > $start_b++;
    > }
    > $start_a++;
    > $start_b="1";
    > $counter_b="255";
    > }

    You need to convert the IP address to a 32 bit integer and back again.

    use Socket;

    my $start_address = unpack 'N', inet_aton( '10.11.1.14' );
    my $finish_address = unpack 'N', inet_aton( '12.13.2.3' );

    for my $address ( $start_address .. $finish_address ) {
    print inet_ntoa( pack 'N', $address );
    }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  3. #3

    Default Re: Enumerating list of IP Addresses from a given range

    In article <3F58160D.87550FEEacm.org>, John W. Krahn wrote:
    > Brian Burns wrote:
    >>
    >> given a start and finish address like this
    >>
    >> $start_address = "10.11.1.14:;
    >> $finish_address = "12.13.2.3";
    >>
    >> I need to output generarate a a sequential list of addresses like the
    >> following:
    >>
    >> 10.11.1.14
    >> 10.11.1.15
    >> 10.11.1.16
    >> ...
    >> 12.13.1.252
    >> 12.13.1.253
    >> 12.13.1.254
    >> 12.13.2.1
    >> 13.13.2.2
    >> 12.13.2.3
    [...]
    >
    > You need to convert the IP address to a 32 bit integer and back again.
    >
    > use Socket;
    >
    > my $start_address = unpack 'N', inet_aton( '10.11.1.14' );
    > my $finish_address = unpack 'N', inet_aton( '12.13.2.3' );
    >
    > for my $address ( $start_address .. $finish_address ) {
    > print inet_ntoa( pack 'N', $address );
    > }
    Aha!

    (But this includes numbers such as 12.13.1.255 and 12.13.2.0 which are not
    in the sample pattern.)

    Here's my less clever (and slower?) method...

    my start_addr = split /\./, $start_address;
    my finish_addr = split /\./, $finish_address;
    my result = start_addr;

    print join ".", result, "\n"; # first number

    while (1) {
    $result[3]++; # increment "ones" by 1
    my $value = join ".", result; # output new number
    print $value, "\n" ;
    last if $value eq $finish_address;

    if ($result[3] == 254) { # need to do carryover
    my $x;
    for ($x = 3; $x > -1; $x--) {
    if ($result[$x] == 254) {
    $result[$x -1]++; # carry over to next column
    $result[$x] = 0; # reset column
    }
    }
    }
    }

    print "\n";

    __END__
    (Comments, suggestions always welcome.)

    -Kevin
    --
    Kevin Pfeiffer

    Kevin Pfeiffer Guest

  4. #4

    Default Re: Enumerating list of IP Addresses from a given range

    Kevin Pfeiffer wrote:
    >
    > In article <3F58160D.87550FEEacm.org>, John W. Krahn wrote:
    >
    > > You need to convert the IP address to a 32 bit integer and back again.
    > >
    > > use Socket;
    > >
    > > my $start_address = unpack 'N', inet_aton( '10.11.1.14' );
    > > my $finish_address = unpack 'N', inet_aton( '12.13.2.3' );
    > >
    > > for my $address ( $start_address .. $finish_address ) {
    > > print inet_ntoa( pack 'N', $address );
    > > }
    >
    > (But this includes numbers such as 12.13.1.255 and 12.13.2.0 which are not
    > in the sample pattern.)
    That depends. They could all be in the 8/5 address space. :-) I take
    it that you are assuming a netmask of 255.255.255.0.


    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  5. #5

    Default Re: Enumerating list of IP Addresses from a given range

    Kevin Pfeiffer wrote:
    >
    > Here's my less clever (and slower?) method...
    >
    > my start_addr = split /\./, $start_address;
    > my finish_addr = split /\./, $finish_address;
    > my result = start_addr;
    >
    > print join ".", result, "\n"; # first number
    >
    > while (1) {
    > $result[3]++; # increment "ones" by 1
    > my $value = join ".", result; # output new number
    > print $value, "\n" ;
    > last if $value eq $finish_address;
    >
    > if ($result[3] == 254) { # need to do carryover
    > my $x;
    > for ($x = 3; $x > -1; $x--) {
    > if ($result[$x] == 254) {
    > $result[$x -1]++; # carry over to next column
    > $result[$x] = 0; # reset column
    > }
    > }
    > }
    > }
    >
    > print "\n";
    >
    > __END__
    > (Comments, suggestions always welcome.)
    A small addition to my original post gives the results you want:

    use Socket;

    my $netmask = 0xFFFFFF00; # 255.255.255.0

    my $start_address = unpack 'N', inet_aton( '10.11.1.14' );
    my $finish_address = unpack 'N', inet_aton( '12.13.2.3' );

    for my $address ( $start_address .. $finish_address ) {
    next if ( $address & $netmask ) == $address or ( $address & ~$netmask ) == ~$netmask;
    print inet_ntoa( pack 'N', $address );
    }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  6. #6

    Default Re: Enumerating list of IP Addresses from a given range

    In article <3F5BC817.DC8E8BAacm.org>, John W. Krahn wrote:
    > Kevin Pfeiffer wrote:
    >>
    >> In article <3F58160D.87550FEEacm.org>, John W. Krahn wrote:
    >>
    >> > You need to convert the IP address to a 32 bit integer and back again.
    [...]
    >> (But this includes numbers such as 12.13.1.255 and 12.13.2.0 which are
    >> not in the sample pattern.)
    >
    > That depends. They could all be in the 8/5 address space. :-) I take
    > it that you are assuming a netmask of 255.255.255.0.
    Ah yes. My example then is actually wrong as my series simply counts to 254
    in each of the four quads. :-(

    Your follow-up solution has gone in my archive.


    --
    Kevin Pfeiffer
    International University Bremen

    Kevin Pfeiffer Guest

  7. #7

    Default Re: Enumerating list of IP Addresses from a given range

    Kevin Pfeiffer wrote:
    >
    > In article <3F5BC817.DC8E8BAacm.org>, John W. Krahn wrote:
    >
    > > Kevin Pfeiffer wrote:
    > >>
    > >> In article <3F58160D.87550FEEacm.org>, John W. Krahn wrote:
    > >>
    > >> > You need to convert the IP address to a 32 bit integer and back again.
    > [...]
    >
    > >> (But this includes numbers such as 12.13.1.255 and 12.13.2.0 which are
    > >> not in the sample pattern.)
    > >
    > > That depends. They could all be in the 8/5 address space. :-) I take
    > > it that you are assuming a netmask of 255.255.255.0.
    >
    > Ah yes. My example then is actually wrong as my series simply counts to 254
    > in each of the four quads. :-(
    >
    > Your follow-up solution has gone in my archive.
    Don't be too quick with that. :-) I did some more testing and on
    Perl 5.6.0 (I know I should upgrade) the range operator doesn't like
    integers larger then 0x7FFFFFFF. So if you have a different version
    of Perl you should test it with a wide range of IP addresses. It
    does however work with a C style for loop.


    use Socket;

    my $netmask = 0xFFFFFF00; # 255.255.255.0

    my $start_address = unpack 'N', inet_aton( '10.11.1.14' );
    my $finish_address = unpack 'N', inet_aton( '12.13.2.3' );

    for ( my $address = $start_address; $address <= $finish_address; ++$address ) {
    next if ( $address & $netmask ) == $address or ( $address & ~$netmask ) == ~$netmask;
    print inet_ntoa( pack 'N', $address );
    }



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  8. #8

    Cool Re: Enumerating list of IP Addresses from a given range

    $start_a = "1";
    $start_b = "1";
    $start_c = "1";
    $start_d = "1";


    $finish_a = "2";
    $finish_b = "3";
    $finish_c = "4";
    $finish_d = "5";


    for ($start_a = 1; $start_a <= $finish_a; $start_a++) {
    for ($start_b = 1; $start_b <= $finish_b; $start_b++) {
    for ($start_c = 1; $start_c <= $finish_c; $start_c++) {
    for ($start_d = 1; $start_d <= $finish_d; $start_d++) {

    $ip_address = join ('.', $start_a,$start_b,$start_c,$start_d);
    print "$ip_address \n ";

    }
    }
    }
    }
    premviswan@gmail.com Guest

  9. #9

    Default Re: Enumerating list of IP Addresses from a given range

    This worked for me:
    sa.sb.sc.sd - fa.fb.fc.fd

    for (my $a = $sa;$a <= $fa;$a++) {
    my $eb = ($a==$fa) ? $fb : 256;
    for (my $b=($a==$sa)?$sb:0;$b<=$eb;$b++) {
    my $ec = ($b==$fb) ? $fc : 256;
    for (my $c=($b==$sb)?$sc:0;$c<=$ec;$c++) {
    my $ed = ($c==$fc) ? $fd : 256;
    for (my $d=($c==$sc)?$sd:0;$d<=$ed;$d++) {
    my $IP = "$a.$b.$c.$d";
    $exIPs{$IP} = 1;
    }
    }
    }
    }
    Matt.Sargent Guest

Similar Threads

  1. Replies: 14
    Last Post: November 24th, 11:26 PM
  2. easier way to list email addresses for multiple recipients
    By Gregg O'Donnell in forum PERL Beginners
    Replies: 1
    Last Post: October 18th, 12:03 AM
  3. enumerating all the paths in acyclic graph
    By Mikito Harakiri in forum IBM DB2
    Replies: 1
    Last Post: August 6th, 03:30 PM
  4. Command or script to get a list of email addresses
    By Deboo in forum PERL Miscellaneous
    Replies: 4
    Last Post: August 5th, 09:22 PM
  5. Replies: 1
    Last Post: August 2nd, 08:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139