Ask a Question related to PERL Beginners, Design and Development.
-
Brian Burns #1
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
-
easier way to list email addresses for multiple recipients
I have a form that needs to send it's data to multiple recipients (35 in all); is there an easier way to do this? <snip> # Region office email... -
Command or script to get a list of email addresses
I have quite many irc chat logs and in many of these, there are email addresses of friends they haf given me during those chats. Can someone help... -
#24909 [Fbk->Opn]: rand function with range always returns low value of range
ID: 24909 User updated by: a0 at hush dot com Reported By: a0 at hush dot com -Status: Feedback +Status: ... -
#24909 [Opn->Fbk]: rand function with range always returns low value of range
ID: 24909 Updated by: iliaa@php.net Reported By: a0 at hush dot com -Status: Open +Status: Feedback... -
#24909 [NEW]: rand function with range always returns low value of range
From: a0 at hush dot com Operating system: solaris 8 PHP version: 4.3.2 PHP Bug Type: Math related Bug description: rand... -
John W. Krahn #2
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
-
Kevin Pfeiffer #3
Re: Enumerating list of IP Addresses from a given range
In article <3F58160D.87550FEE@acm.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
Aha!>
> 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.)
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
-
John W. Krahn #4
Re: Enumerating list of IP Addresses from a given range
Kevin Pfeiffer wrote:
That depends. They could all be in the 8/5 address space. :-) I take>
> In article <3F58160D.87550FEE@acm.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.)
it that you are assuming a netmask of 255.255.255.0.
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
John W. Krahn #5
Re: Enumerating list of IP Addresses from a given range
Kevin Pfeiffer wrote:
A small addition to my original post gives the results you want:>
> 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.)
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
-
Kevin Pfeiffer #6
Re: Enumerating list of IP Addresses from a given range
In article <3F5BC817.DC8E8BA@acm.org>, John W. Krahn wrote:
[...]> Kevin Pfeiffer wrote:>>
>> In article <3F58160D.87550FEE@acm.org>, John W. Krahn wrote:
>>>> > You need to convert the IP address to a 32 bit integer and back again.
Ah yes. My example then is actually wrong as my series simply counts to 254>>> (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.
in each of the four quads. :-(
Your follow-up solution has gone in my archive.
--
Kevin Pfeiffer
International University Bremen
Kevin Pfeiffer Guest
-
John W. Krahn #7
Re: Enumerating list of IP Addresses from a given range
Kevin Pfeiffer wrote:
Don't be too quick with that. :-) I did some more testing and on>
> In article <3F5BC817.DC8E8BA@acm.org>, John W. Krahn wrote:
>> [...]> > Kevin Pfeiffer wrote:> >>
> >> In article <3F58160D.87550FEE@acm.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.
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
-
premviswan@gmail.com #8
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



Reply With Quote

