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
https://forums.phpfreaks.com/topic/60404-loop-issues/
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
https://forums.phpfreaks.com/topic/60404-loop-issues/#findComment-300480
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
https://forums.phpfreaks.com/topic/60404-loop-issues/#findComment-301141
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
https://forums.phpfreaks.com/topic/60404-loop-issues/#findComment-301523
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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