Problem with simple for and gethostbyaddr Plz Help

Discussion in 'PHP' started by Katanius, Mar 26, 2007.

  1. #1
    Hellow,
    I've writen a simple php script that scans an ip range and returns the hosts but the problem is that it just stops returning results for no obvius reason to me.. I really cant figure whats wrong.. Im not realy experienced in PHP so plz, could anyone help me with this?

    <?php
    
    $startofrange='62.112.192.1';
    $endofrange='62.112.192.50';
    
    $ipstart=ip2long($startofrange);
    $ipend=ip2long($endofrange);
    
    for( $ip=$ipstart ; $ip<=$ipend ; $ip++ )
    {
    echo long2ip($ip)."=";
    echo $ip."=";
    $hostname = gethostbyaddr($ip);
    echo $hostname."<br>";
    }
    
    ?>
    
    PHP:
    Any ideas? what am i doing wrong?
     
    Katanius, Mar 26, 2007 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    change
    $hostname = gethostbyaddr($ip);
    PHP:
    to
    $hostname = gethostbyaddr(long2ip($ip));
    PHP:



    script ran fine on my end. if it still doesnt work for you,
    it might be PHP settings on your server.
     
    lbalance, Mar 26, 2007 IP
  3. Katanius

    Katanius Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried that but it didnt work,

    then i checked the settings and max_execution_time was set to 30sec so the code couldnt complete in such sort time. I changed it to half an hour an it worked fine :)

    Thank you for your help!
     
    Katanius, Mar 26, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    
    <?php
    /** Set a sensible line break, coz it makes sense **/
    $break = php_sapi_name() == "cli" ? "\r\n" : "<br />\n";
    /** Make a function out of it **/
    function scan_range( $start, $finish, $print = true )
    {
    	/** Need line breaks **/
    	global $break;
    	/** Always begin at the beginning **/
    	$data["start"] = ip2long( $start ) ;
    	/** Try to finish at the end **/
    	$data["finish"] = ip2long( $finish ) ;
    	do /** START DOOOING **/
    	{	
    		$ip = long2ip( $data["start"] );
    		$data["results"][$ip]["long"] = @ip2long( $ip );
    		$data["results"][$ip]["host"] = @gethostbyaddr( $ip );
    		/** ONLY OUTPUT IF $PRINT == TRUE **/
    		if( $print ):
    			printf("IP : %s %s",  $ip, $break );
    			printf("Long : %s %s",  $data["results"][$ip]["long"], $break );
    			printf("Host : %s %s",  $data["results"][$ip]["host"], $break );
    		endif;
    		/** CONTINUE DOING **/
    		$data["start"]++;		
    	}
    	/** WHILE THIS STATEMENT IS TRUE **/
    	while( $data["start"] <= $data["finish"] );
    	/** PUT THINGS BACK THE WAY WE FOUND IT **/
    	$data["start"] = $start;
    	$data["finish"] = $finish;
    	/** RETURN THE ARRAY OF DATA **/
    	return $data ;	
    }
    // will print formatted data
    scan_range('62.112.192.1', '62.112.192.10' );
    // will store results in $array without printing
    $array = scan_range('62.112.192.1', '62.112.192.10', false );
    // looping over that array
    foreach( $array["results"] as $key => $data )
    {
    	echo "IP : $key $break";
    	echo "Long : $data[long] $break";
    	echo "Host : $data[host] $break";
    }
    echo "<*RAW*> $break";
    print_r( $array );
    echo "<*RAW*> $break";
    ?>
    
    PHP:
    That was fun .....
     
    krakjoe, Mar 27, 2007 IP
  5. Katanius

    Katanius Peon

    Messages:
    12
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nice code krackjoe thanx very much,
    it seem a bit complex to me but it will do me good to figure it out and learn
    from it, as i said before im not realy expirienced in php :)

    As i've been working with gethostbyaddr i noticed that when there are dead hosts in the scanned range it takes really long to return results to the browser,
    about 3 secs per dead host, so when i tryied to scan a range of about 500 ips it
    took a bit longer than half an hour!!

    is there any way to make the gethostbyaddr go faster? or at least not delay that much on every dead host. should i forget gethostbyaddr and try something else?:rolleyes:
     
    Katanius, Mar 29, 2007 IP