1

ルーター(192.168.200.254)の背後にある企業LAN上の2台のコンピューター(192.168.200.1と192.168.200.2)にpingを実行しています。

function pingAddress($TEST) {
$pingresult = exec("ping -n 1 $TEST", $output, $result);
    if ($result == 0) {
        echo "Ping successful!";
        } else {
        echo "Ping unsuccessful!";
        }

    }
pingAddress("192.168.220.1");
pingAddress("192.168.220.2");

私の問題は、これらのコンピューターの1つに電源がオンになっていない(.1)にもかかわらず、ping応答が返されることです。

Pinging 192.168.200.1 with 32 bytes of data:
Reply from 192.168.200.254: Destination host unreachable.
Ping statistics for 192.168.200.1:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),

192.168.220.1のping試行でのvar_dump($ output)は、次のように表示されます。

array(6) { 
[0]=> string(0) "" 
[1]=> string(44) "Pinging 192.168.200.1 with 32 bytes of data:" 
[2]=> string(57) "Reply from 192.168.200.254: Destination host unreachable." 
[3]=> string(0) "" 
[4]=> string(34) "Ping statistics for 192.168.200.1:" 
[5]=> string(56) " Packets: Sent = 1, Received = 1, Lost = 0 (0% loss)," 
}

そのため、代わりに、誤検知の「宛先ホストに到達できません」メッセージに対して作成された$ output配列を検索しようとしていますが、このルートではうまくいきません。

function pingAddress($TEST) {
$findme ="Destination host unreachable";

    $pingresult = exec("ping -n 1 $TEST  && exit", $output, $result);
        //echo $result. "<br/>";
        if (($result == 0) AND (in_array($findme, $output))){
            echo "Ping unsuccessful! <br/>";
        }
        elseif (($result == 0) AND (!in_array($findme, $output))){
            echo "Ping successful! <br/>";
        }
        elseif ($result == 1){
            echo "Ping unsuccessful! <br/>";
        }    
}
pingAddress("192.168.220.1");
pingAddress("192.168.220.2");

それでも成功したと表示されます。私はおそらくここで何か間違ったことをしています。何か案は?

4

2 に答える 2

1

必要なのはpreg_grepです。これを試してみてください:

function pingAddress($TEST) {
    $pingresult = exec("ping -n 1 $TEST  && exit", $output, $result);
    //echo $result. "<br/>";

    if (($result == 0)){
        if(count(preg_grep('/Destination host unreachable/i', $output)) == 0){
            echo "Ping successful! <br/>";
        else
            echo "Ping unsuccessful! <br/>";
    }
    elseif ($result == 1){
        echo "Ping unsuccessful! <br/>";
    }    

}

于 2013-03-11T19:19:26.350 に答える
0

in_arrayは、文字列全体、つまり「192.168.200.254からの応答:宛先ホストに到達できません」を予期します。。strstr 代わりに、 php手動strstrphp関数または正規表現チェックを使用できます。そしてそれは配列にあるので-誰かが提案したように..配列の文字列を結合してテキストを見つけようとします。

于 2013-03-11T19:19:27.950 に答える