2012-03-01 31 views
1

我有一個問題在PHP上執行'ping',我收到一個空白的結果,但如果我執行其他命令就像一個'whoami'我收到了正確的結果,你能請幫幫我?任何想法?Centos 6.2 - Apache 2 - PHP 5 - 執行空白結果

<?php 
exec('ping google.com', $output); 
echo $output; 
//Result: 
?> 

<?php 
exec('whoami', $output); 
echo $output; 
//Result: apache 
?> 

感謝

注:或許可以從一些Apache的配置?或PHP配置?還是linux權限?

+0

我想指出,'$ output'將是一個數組,所以你不能只是直接呼應它,如果輸出將跨越多行(如與平)。 – 2012-03-01 14:56:18

+0

是的,我嘗試過,但不起作用,'<?php echo exec('ping google.com'); //結果: ?>' – AlejandroQH 2012-03-01 15:02:35

回答

0

試試這個代碼。

<?php 
function GetPing($ip=NULL) { 
if(empty($ip)) {$ip = $_SERVER['REMOTE_ADDR'];} 
if(getenv("OS")=="Windows_NT") { 
    $exec = exec("ping -n 3 -l 64 ".$ip); 
    return end(explode(" ", $exec)); 
} 
else { 
    $exec = exec("ping -c 3 -s 64 -t 64 ".$ip); 
    $array = explode("/", end(explode("=", $exec))); 
    return ceil($array[1]) . 'ms'; 
} 
} 

echo GetPing(); 
?> 
+0

謝謝,但在所有情況下,我收到0ms,即使外部主機像8.8.8.8(谷歌的DNS) – AlejandroQH 2012-03-01 14:53:11

+0

你試過這回聲GetPing(「google.com」); ? – Milap 2012-03-01 14:55:40

+0

對不起,但同樣的歷史,結果:0ms pinging到google.com – AlejandroQH 2012-03-01 15:05:19

1

我猜這是因爲ping的默認行爲是永不停止。它一直持續下去,直到你殺了它。

人平「發送計數ECHO_REQUEST包後-c計數 停止,隨着最後期限的選擇,平等待計數ECHO_REPLY數據包,直到 超時。」說

嘗試添加選項來限制ping運行時間並查看是否得到結果。

+0

我並列但不起作用。我綁在運行一個簡單的命令ping(Wws(Windows操作系統)和完美運行,但在Linux不 – AlejandroQH 2012-03-01 14:58:43

0

你也可以試試這個

<?php 
$output = shell_exec('ping google.com'); 
echo "<pre>$output</pre>"; 
?> 
+0

謝謝,但不工作只有服務器響應此HTML。 「

」 –
                        
                            
    AlejandroQH
                                2012-03-01 15:00:01