Question

i want to have a php code that ping two servers then if these servers are online php code echo Online

This is my code :

$ip = "IP1"; //IP or web address
$port = "22"; //Port
$ip2 = "IP2"; //IP or web address
$port2 = "22"; //Port
$sock = @fsockopen( $ip, $port, $num, $error, 5 ); //2 is the ping time, you can sub what you need
$sock2 = @fsockopen( $ip2, $port2, $num2, $error2, 5 ); //2 is the ping time, you can sub what you need
if( !$sock & !$sock2 ){
//Do this if it is closed
    echo '<img title="Offline" src="../images/down.png">';
    }
if( $sock & $sock2 ){
//Do this if it is open
    echo '<img title="Online" src="../images/up.png">';
    fclose($sock);
    fclose($sock2);
}
?>

Is this the correct code ?

Was it helpful?

Solution

Your code has a few very minor issues: if one sock is open and the other is not, you will not close the open socket (PHP will probably close it at the end of the script anyway), because you close them only if both are open. You used binary and (&) instead of the logical and && if( $sock && $sock2 ) {, and ending image tags with /> is always a good idea to make it compliant with every HTML flavour.

Here is a multiserver version:

<?php

    $SERVERS_TO_TEST=array(
        array(
            'IP'=>'192.168.100.1',
            'PORT'=>22,
            'TIMEOUT'=>5,
        ),
        array(
            'IP'=>'192.168.100.101',
            'PORT'=>22,
            'TIMEOUT'=>5,
        ),
    );

    $ALL_ONLINE=TRUE;
    $COUNT_ONLINE=0;
    $COUNT_ALL=count($SERVERS_TO_TEST);
    foreach($SERVERS_TO_TEST as $aServer) {
        $ip = $aServer['IP'];
        $port = $aServer['PORT'];
        $timeout = $aServer['TIMEOUT'];

        $errNum=0;
        $errStr='';

        $sock = fsockopen( $ip, $port, $errNum, $errStr, $timeout );
        if($sock!==FALSE) {
            fclose($sock1);
            $COUNT_ONLINE++;
        } else $ALL_ONLINE=FALSE;
    }

    if($ALL_ONLINE){
        echo '<img alt="All online" title="All online" src="../images/up.png" />';
    } else {
        $perc=($COUNT_ALL-$COUNT_ONLINE).'/'.$COUNT_ALL;
        echo '<img alt="Offline '.$perc.'" title="Offline '.$perc.'" src="../images/down.png" />';
    }
?>

If you test this way for many servers, this will take a while, but there are ways to make it run all tests in parallel even from a webserver (without threads), you have to make a single script to test for one server "seeifonline.php" (the server is a parameter to the url) and then open that script many times with nonblocking sockets to localhost,80 (they will reply immediately) and manually writing 'GET seeifonline.php?sever=$server' with a different $server to each one. Wait in a loop for all the replies. It's complicated but it works and you can check the status of say 20 servers in the same time as you do for one (5 seconds timeout). The main difference is that you know your localhost server is online, so all those fsockopens to localhost will reply immediately and launch the script. The script calls fsockopen to one remote host, and it it hangs till timeout, the other scrips will go on like independent threads.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top