Jump to content

Help with Ping script


robembra

Recommended Posts

Hi,

 

I want to make a ping script but have no experience with the exec or system functons. I have a script but it seems to only work with hostnames and not IP's.

 

$host = "127.0.0.1";
echo "<title>$host ping test</title>";

function do_ping($host, $fping, $p_result) {
$fping = exec("ping $host -c1 -w2 | awk '/time/ {print $8}' | head -1 | awk -F '=' '{print $2}'");

if ($fping <= "75" && $fping >= "0") {
$p_result = "<font color=green>GOOD</font>";
} else if ($fping <= "150" && $fping > "75") {
$p_result = "<font color=yellow>AVERAGE</font>";
} else if ($fping > "150") {
$p_result = "<font color=red>BAD</font>";
} else if ($fping == "") {
echo "<font face=Verdana>Host <b>$host</b> is not responding to ping.</font>";
exit;
}

echo "<font face=Verdana>Latency to host <b>$host</b> is <b>$fping ms.</b> This is <b>$p_result.</b></font>";
}

do_ping($host, $fping, $p_result)

 

Also I would like the output to be put into an array so i can retreve it easily.

 

E.g of Output....

 

Array
(
    [PACKETS] => Array
        (
            [sENT] => 4
            [RECEIVED] => 4
            [LOST] => 0
        )
    [TIME] => Array
        (
            [MIN] => 44
            [MAX] => 68
            [AVE] => 52
        )
)

 

If this cannot be done, the Average ping response is the value I must have from the output.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/182714-help-with-ping-script/
Share on other sites

the output of pinging a domain and a ip are different. 

you'd have to examine the output and build your command based on whether it's an IP or domain

example

domain

PING www.l.google.com (74.125.95.105) 56(84) bytes of data.
64 bytes from iw-in-f105.1e100.net (74.125.95.105): icmp_seq=1 ttl=53 time=43.8 ms

IP

1 packets transmitted, 1 received, 0% packet loss, time 60ms
rtt min/avg/max/mdev = 60.595/60.595/60.595/0.000 ms

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.