robembra Posted November 23, 2009 Share Posted November 23, 2009 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 More sharing options...
taquitosensei Posted November 23, 2009 Share Posted November 23, 2009 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 Link to comment https://forums.phpfreaks.com/topic/182714-help-with-ping-script/#findComment-964382 Share on other sites More sharing options...
robembra Posted November 24, 2009 Author Share Posted November 24, 2009 Hi, It will be an IP so what should I change in the exec(). I have no knowledge of this function and what is it calling, i got it from an example. So what do I have to change to ping an IP and recover the time it took. Thanks Link to comment https://forums.phpfreaks.com/topic/182714-help-with-ping-script/#findComment-964390 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.