2012-11-08 406 views

回答

17
$ip = "127.0.0.1"; 
exec("ping -n 3 $ip", $output, $status); 
print_r($output); 

輸出看起來像下面

Array 
(
    [0] => 
    [1] => Pinging 127.0.0.1 with 32 bytes of data: 
    [2] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 
    [3] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 
    [4] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 
    [5] => 
    [6] => Ping statistics for 127.0.0.1: 
    [7] =>  Packets: Sent = 3, Received = 3, Lost = 0 (0% loss), 
    [8] => Approximate round trip times in milli-seconds: 
    [9] =>  Minimum = 0ms, Maximum = 0ms, Average = 0ms 
) 
+6

我得到了這樣的空陣列, Array() 你能幫助我嗎? –

+1

如果你有空數組'Array()'嘗試使用'-c 3'而不是'-n 3'。您可能正在運行Linux服務器而不是Windows。 Linux ping無法識別'-n'選項。 – Palo

+0

確實3意味着命令將被執行三次? –

11

試試這個

$host="192.168.0.104"; 

exec("ping -c 4 " . $host, $output, $result); 

print_r($output); 

if ($result == 0) 

echo "Ping successful!"; 

else 

echo "Ping unsuccessful!"; 

注:這取決於你所使用的操作系統上。 Windows將默認只有4個ping,而Linux將永遠ping。

要在Windows中執行ping兩次,使用 「ping -n 2主機」

要在Linux ping通兩次,使用 「ping -c 2主機」

1

我只是ping通谷歌與exec

<?php 
echo exec("ping www.google.com"); 
?> 

產量爲:

Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),