Question

I need to more or less obtain a table, that reports an IP of a machine on the local network, and if it can be found or not.

I have set up 20 machines, all with static IP address, from 192.168.1.111 - 192.168.1.120

How can I check to see if the machine is 'online' and report it's status back to the web page?

Currently, I'm doing this:

<?php

    $nodes = array(
        "192.168.1.111",
        "192.168.1.112",
        "192.168.1.113",
        "192.168.1.114", // offline
        );

    $table = "";

    for($i = 0; $i < count($nodes); ++$i) {
        exec("ping -c 4 " . $nodes[$i], $output, $result);

        if ($result == 0) {
            // echo "Ping successful!";      
            $table += "<tr>";
            $table += "<td>Node " . $i . "</td>";
            $table += "<td> " . $nodes[$i] . "</td>";
            $table += "<td>Ready</td>";
            $table += "</tr>";

        } else {
            // echo "Ping unsuccessful!";     
            $table += "<tr>";
            $table += "<td>Node " . $i . "</td>";
            $table += "<td> " . $nodes[$i] . "</td>";
            $table += "<td>Offline</td>";
            $table += "</tr>"; 
        }
    }   

    echo $table;

?>

I am new to php, so please forgive me if something is wrong (I'm sure it is) but, this doesn't output the data correctly, and it also takes a very long time to 'ping back' to the local web site.

The above script is in it's own file, and included with <?php include('nodes.php'); ?>

Idealy, the information should be displayed like the following:

Computer Name             IP Address        Status
Node 1                    192.168.1.111     Online
Node 2                    192.168.1.112     Online
Node 3                    192.168.1.113     Online
Node 4                    192.168.1.114     Offline
Was it helpful?

Solution

Please try the following, or using /bin/ping -n 3

<?php
function pingAddress($ip) {
    $pingresult = exec("/bin/ping -n 3 $ip", $outcome, $status);
    if (0 == $status) {
        return = True;
    } else {
        return = False;
    }
}

pingAddress("127.0.0.1");
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top