0

I'm trying to ping the ip address to test the ip address whether it is available or not. But i having trouble on using the fsockopen function. I had do some research on using 'ping','exec' function but none of them seem really workable.

Here is my code to test the IP address:

<?php
    $ipAddress = array("192.168.1.1","192.168.1.67");
    $kiosk = array("Kuantan (35)","UTC Kuantan (36)");

    $checkCount = 0;
    foreach(array_combine($ipAddress,$kiosk) as $items => $kiosk){
        $fp = @fSockOpen($items,80,$errno,$errstr,1);

        if(is_resource($fp)){
            if($fp) { 
                $status=0; 
                fclose($fp); 
                echo 'Success<br/>'; 
            }
        }
        else{
            echo 'Failed<br/>'; 

        }

    }
?>

Both the IP address i can ping from cmd prompt. But for 192.168.1.67 it lead me to 'Failed', only the 192.168.1.1. or 127.0.0.1 showing me 'Success'. Is there anything i do wrong?

Marcus Tan
  • 407
  • 1
  • 9
  • 26
  • related? http://stackoverflow.com/questions/9841635/how-to-ping-a-server-port-with-php – Catalyst Apr 23 '15 at 05:42
  • @Catalyst yes, i found the solution from there and trying to do for my own, but now i'm facing a problem with the code. It was like some of the responsive ip address are shown failed in my code when i'm trying to ping. Is there anything i do wrong in my code? Sorry for my poor english – Marcus Tan Apr 23 '15 at 05:46
  • Gotta reinstall PHP on my linux node, I'll try it out and let you know what happens. – Catalyst Apr 23 '15 at 05:50
  • @Catalyst Okay thanks. I currently have 192.168.1.67 that can response when i'm pinging, but my code echo 'Failed'. I'm not sure why – Marcus Tan Apr 23 '15 at 05:54
  • when I try with a google server (I pinged google from command line and this server responded) 74.125.224.7 your script with no changes returns success. – Catalyst Apr 23 '15 at 05:55
  • @Catalyst Yes, some of the ip address can ping success, but some of the ip address which can be ping in cmd prompt are showing 'Failed' when i execute my code. Is there any setting i had to change? – Marcus Tan Apr 23 '15 at 05:57
  • My thought is that you are running the server outside of your house, but pinging ip addresses within an internal network. For example, my router will assign addresses to a subnet for my apartment, starting at 192.168.1.1, then incrementing to 192.168.1.2 and so on --- but from the outside of my apartment, only the address of my router is visible - you can see this address by googling "what's my ip". If there is no ping then, then it may be because your router is not configured to respond to pings for security reasons. – Catalyst Apr 23 '15 at 06:02
  • @Catalyst The address for 192.168.1.67 just right beside me only, and i'm trying this within an internal network. What may causes the failed to occur? – Marcus Tan Apr 23 '15 at 06:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75996/discussion-between-catalyst-and-marcus-tan). – Catalyst Apr 23 '15 at 06:13

1 Answers1

0

Your code is not really pinging but just tries to open a TCP connection on port 80. The ping command uses ICMP packets, so probably 192.168.1.67 is not accepting connections on port 80.

Following your chat with Catalyst here is a sample code that will send only two packets and wait up to 2 seconds for a response. $retval will be 0 on success, 1 on packet loss, 2 on any other error. This is on Linux, the windows command parameters are bit different but you should be able to change it.

<?
// unset variables first to avoid mixing the results from previous calls
$retval=-1;
$output=array();
exec("ping 127.0.0.1 -c2 -w2 2>&1",$output,$retval);
echo "Return code: ".$retval."<br>\n";
echo implode("<br>\n",$output);
?>
Dobromir Velev
  • 530
  • 2
  • 15