Jump to content

Loop issues


jgreeno

Recommended Posts

I'm trying to setup a page for our techs to use to determine which IPs are free in our DNS in a subnet that they supply.

 

For instance they would type in 192.168.1 to see all free IPs in the 192.168.1.1-255 range.

Here's my issue:

I've tried the while, do_while and for and still the script only runs to the 42nd address before quitting.

What am I missing?

 

 

$l="0";
$finalp=".";	
for($l<=255; ;$l++){
$ipaddress=$record.$finalp.$l;
$lookup=@gethostbyaddr($ipaddress);
echo "<br>".$ipaddress." scanned.<br>";
if ($lookup==$ipaddress){
$data.="<b>".$ipaddress."</b> is free<br>";
echo "<b>".$ipaddress."</b> is free<br>";
					}
				}	

Link to comment
Share on other sites

<?php

  $finalp=".";	
  foreach(range(0,255) as $l) {
    $ipaddress = $record.$finalp.$l;
    $lookup=@gethostbyaddr($ipaddress);
    echo "<br>".$ipaddress." scanned.<br>";
    if ($lookup==$ipaddress){
      $data.="<b>".$ipaddress."</b> is free<br>";
      echo "<b>".$ipaddress."</b> is free<br>";
    }
  }

?>

Link to comment
Share on other sites

After some more troubleshooting I'm seeing that the loop works fine, it appears to be an issue with the amount of data that I'm pumping into the variable $data.

After looking up only 42 addresses, the loop exits and does not execute subsequent commands.

It's like the script just quits after 42 entries.

 

 

Is there a logical limit to the amount of data that can be stored in a variable?

Link to comment
Share on other sites

Am I even on the right track here?

 

I feel like I'm so close and missing something stupid and simple..

 

 

<?php
// Initialize some variables
$finalp=".";
$loop="0";
$record="192.168.1";

// 0 - 255 step loop
while($loop <=255){
// Build the ip from the $record and the loop counter
$ipaddress=$record.$finalp.$loop;
// Do a lookup on the address
$data=@gethostbyaddr($ipaddress);
// If the lookup pukes, push the ip to an array value with loop counter as the key
if ($data==$ipaddress){
$arr=array($loop => $ipaddress);
                      }
// Increment the counter
$loop++;
}
// Feed the array to foreach and build the output
foreach($arr as $output){
$output.=$output;
}
?>

Link to comment
Share on other sites

also,

at the end of every look, maybe go unset($date); or maybe create an array value, that will store all your results?

 

So, how can I write values out of the loop and into an array for later use?

My use of an array above seems to be overwritten every go round..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.