如何为(ping命令使用)PHP创建日志文件?

时间:2017-11-10 16:12:37

标签: php

请使用此函数使用PHP运行ping命令,我想在详细信息中为ping命令创建日志文件。请尽可能地告诉我。 我的代码

<?php $host=array(
    "test6"  => "127.0.0.1",
    "test5" =>"98.139.180.149",
    "test4" =>"192.168.0.60",
    "test3"  =>"172.217.23.78",
    "test2"  =>"192.168.0.62",
    "test1" =>"184.168.221.104",
); 
$port="";
foreach ($host as $key => $value) { //  $output=shell_exec('ping -n 1 '.$value);  
    if (!$socket = @fsockopen(  $value, 80, $errno, $errstr,  1)) {
        echo '   <table border="1" >
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> offline</td>

        </table>';
    } else {
        echo '   <table border="2">
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> online</td>

        </table>';


    fclose($socket); 
    };
};

1 个答案:

答案 0 :(得分:0)

假设您要将表写入日志文件:

<?php $host=array(
    "test6"  => "127.0.0.1",
    "test5" =>"98.139.180.149",
    "test4" =>"192.168.0.60",
    "test3"  =>"172.217.23.78",
    "test2"  =>"192.168.0.62",
    "test1" =>"184.168.221.104",
); 

$res = fopen('/path/to/logfile.log',"a");

foreach ($host as $key => $value) {
    if (!$socket = @fsockopen(  $value, 80, $errno, $errstr,  1)) {
        $table = '   <table border="1" >
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> offline</td>

        </table>';
        fwrite(res, $table . "\n");
    } else {
        $table = '   <table border="2">
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> online</td>

        </table>';
        fwrite(res, $table . "\n");
        fclose($socket);
    }

}
fclose($res);

编辑:更改为考虑@ Phillip的评论