For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > printing a range of ip addresses









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author printing a range of ip addresses
Harold Castro

2006-03-22, 3:57 am

Hi,

Can you tell me why this loop doesn't work???

#!/usr/local/bin/perl
use warnings;
use strict;

our $hostpart = 1;
our $networkpart = 128;
$|=1;

while ($networkpart <= 158){
while ($hostpart <= 256){
print "202.90.".$networkpart.".".$hostpart, "\n";
++$hostpart;
}
++$networkpart;
}

It only prints the range from 202.90.128.0 - 202.90.128.256.
I want it print upto 202.90.158.256.


Thanks..


---------------------------------
Yahoo! Mail
Bring photos to life! New PhotoMail makes sharing a breeze.
Alon Marx

2006-03-22, 3:57 am

Try setting the $hostpart to 1 just before the inner while loop :)



On Wed, 2006-03-22 at 01:20 -0800, Harold Castro wrote:
> Hi,
>
> Can you tell me why this loop doesn't work???
>
> #!/usr/local/bin/perl
> use warnings;
> use strict;
>
> our $hostpart = 1;
> our $networkpart = 128;
> $|=1;
>
> while ($networkpart <= 158){
> while ($hostpart <= 256){
> print "202.90.".$networkpart.".".$hostpart, "\n";
> ++$hostpart;
> }
> ++$networkpart;
> }
>
> It only prints the range from 202.90.128.0 - 202.90.128.256.
> I want it print upto 202.90.158.256.
>
>
> Thanks..
>
>
> ---------------------------------
> Yahoo! Mail
> Bring photos to life! New PhotoMail makes sharing a breeze.

Balan Ranganathan

2006-03-22, 8:00 am


I am seeing code optimization done inside the while loop.

Thanks

Best regards
Bala


-----Original Message-----
From: Harold Castro [mailto:b0ydaem0n@yahoo.com]=0D
Sent: Wednesday, March 22, 2006 2:50 PM
To: beginners@perl.org
Subject: printing a range of ip addresses

Hi,
=0D
Can you tell me why this loop doesn't work???
=0D
#!/usr/local/bin/perl
use warnings;
use strict;
=0D
our $hostpart =3D 1;
our $networkpart =3D 128;
$|=3D1;
=0D
while ($networkpart <=3D 158){
while ($hostpart <=3D 256){
print "202.90.".$networkpart.".".$hostpart, "\n";
++$hostpart;
}
++$networkpart;
}
=0D
It only prints the range from 202.90.128.0 - 202.90.128.256.
I want it print upto 202.90.158.256.
=0D
=0D
Thanks..
=0D
=0D
---------------------------------
Yahoo! Mail
Bring photos to life! New PhotoMail makes sharing a breeze.=0D


The information contained in this electronic message and any attachments to=
this message are intended for the exclusive use of the addressee(s) and=
may contain proprietary, confidential or privileged information. If you=
are not the intended recipient, you should not disseminate, distribute or=
copy this e-mail. Please notify the sender immediately and destroy all=
copies of this message and any attachments.=0D

WARNING: Computer viruses can be transmitted via email. The recipient=
should check this email and any attachments for the presence of viruses.=
The company accepts no liability for any damage caused by any virus=
transmitted by this email.
=0D
www.wipro.com
T Baetzler

2006-03-22, 8:00 am

Hi,=20

Harold Castro <b0ydaem0n@yahoo.com> asked:

> Can you tell me why this loop doesn't work???
> =20
> #!/usr/local/bin/perl
> use warnings;
> use strict;
> =20
> our $hostpart =3D 1;
> our $networkpart =3D 128;


That should probably be "my" instead of "our".

> $|=3D1;
> =20
> while ($networkpart <=3D 158){


You should probably set $hostpart to 1 here, or else the
inner loop will run just once up to 256, and then never
again. In any case, a valid octet must be in the range=20
of 0..255.

> while ($hostpart <=3D 256){
> print "202.90.".$networkpart.".".$hostpart, "\n";
> ++$hostpart;
> }
> ++$networkpart;
> }


How about

#!/usr/bin/perl -w

use strict;
use warnings;

for( my $networkpart =3D 128; $networkpart <=3D 158; $networkpart++ ){
for( my $hostpart =3D 0; $hostpart < 256; $hostpart++ ){
print "202.90.$networkpart.$hostpart\n";
}
}
__END__

And if you really, really wanted to do things right you might
have to consider the netmask(s) for the IP range you're looking
at, because these decide what adresses are actually valid.

HTH,
Thomas
Nishanth Ev

2006-03-22, 8:00 am


Edit your loop like this


while ($networkpart <= 158){
while ($hostpart <= 256){
print "202.90.".$networkpart.".".$hostpart, "\n";
$hostpart++;
}
#
#Change the $hostpart back to 0
#
$hostpart=0;
$networkpart++;
}


--- Harold Castro <b0ydaem0n@yahoo.com> wrote:

> Hi,
>
> Can you tell me why this loop doesn't work???
>
> #!/usr/local/bin/perl
> use warnings;
> use strict;
>
> our $hostpart = 1;
> our $networkpart = 128;
> $|=1;
>
> while ($networkpart <= 158){
> while ($hostpart <= 256){
> print "202.90.".$networkpart.".".$hostpart,
> "\n";
> ++$hostpart;
> }
> ++$networkpart;
> }
>
> It only prints the range from 202.90.128.0 -
> 202.90.128.256.
> I want it print upto 202.90.158.256.
>
>
> Thanks..
>
>
> ---------------------------------
> Yahoo! Mail
> Bring photos to life! New PhotoMail makes sharing a
> breeze.



________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Jeff Pang

2006-03-22, 8:00 am


>How about
>
>#!/usr/bin/perl -w
>
>use strict;
>use warnings;
>
>for( my $networkpart = 128; $networkpart <= 158; $networkpart++ ){
> for( my $hostpart = 0; $hostpart < 256; $hostpart++ ){
> print "202.90.$networkpart.$hostpart\n";
> }
>}
>__END__
>


The '-w' option is not needed here since you have 'use warnings'.

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
Paul Lalli

2006-03-22, 8:00 am

T Baetzler wrote:
> Hi,
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;


It's excellent that you chose to enable warnings, but enabling them
twice is something of an overkill. :-P

> for( my $networkpart = 128; $networkpart <= 158; $networkpart++ ){
> for( my $hostpart = 0; $hostpart < 256; $hostpart++ ){
> print "202.90.$networkpart.$hostpart\n";
> }
> }


Ugh. So much typing. Don't try to program C in Perl. Use Perl for
all it's worth:

for my $networkpart (128..158) {
for my $hostpart (0..255) {
print "202.90.$networkpart.$hostpart\n";
}
}

Paul Lalli

Uri Guttman

2006-03-22, 6:58 pm

>>>>> "PL" == Paul Lalli <mritty@gmail.com> writes:

PL> T Baetzler wrote:
[color=darkred]

PL> Ugh. So much typing. Don't try to program C in Perl. Use Perl for
PL> all it's worth:

PL> for my $networkpart (128..158) {
PL> for my $hostpart (0..255) {
PL> print "202.90.$networkpart.$hostpart\n";
PL> }
PL> }

same to you! :)

for my $networkpart (128..158) {
print map( "202.90.$networkpart.$_\n", 0..255 ) ;
}

and that could be scrunched into nested maps if you nutty enough. i
leave that for the trolls to do.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
John W. Krahn

2006-03-22, 6:58 pm

Harold Castro wrote:
> Hi,


Hello,

> Can you tell me why this loop doesn't work???
>
> #!/usr/local/bin/perl
> use warnings;
> use strict;
>
> our $hostpart = 1;
> our $networkpart = 128;
> $|=1;
>
> while ($networkpart <= 158){
> while ($hostpart <= 256){
> print "202.90.".$networkpart.".".$hostpart, "\n";
> ++$hostpart;
> }
> ++$networkpart;
> }
>
> It only prints the range from 202.90.128.0 - 202.90.128.256.
> I want it print upto 202.90.158.256.


You need to have two nested loops:

for my $networkpart ( 128 .. 158 ) {
for my $hostpart ( 1 .. 256 ) {
print "202.90.$networkpart.$hostpart\n";
}
}



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com