本文介绍了需要使用PHP ping跟踪PC的停机时间并显示D:HH:MM的停机时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有这段代码,可以作为ph值运行,可以让我知道计算机是否可以ping通:

I currently have this code, which runs as a ph that works to let me know if the pc's are pinging:

    <!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="30">
</head>
<body>

<h1>PC Test Ping Status</h1>

<?php
$host="10.161.10.98";
exec("ping -c 2 " . $host, $output, $result);
if ($result == 0)
echo "<p>p2 On-Line</p>";
else
echo "<p>p2 Off-Line !</p>";

$host="10.161.10.125";
exec("ping -c 2 " . $host, $output, $result);
if ($result == 0)
echo "<p>p3 On-Line</p>";
else
echo "<p>p3 Off-Line!</p>";

?>


</body>
</html>

我想跟踪自上次成功ping以来的时间(如果PC不能ping通).

I want to track the time since the last successful ping if the pc isn't pinging.

推荐答案

以下是根据要求使用文本文件的示例.一些注意事项:

Here is an example using a text file, as requested. A few notes:

  1. 为简单起见,我建议使用CURL代替exec,因为它应该更快,更可靠.这将检查HTTP状态代码"200",这表示它返回了有效的请求.
  2. 您需要确保您的文本文件具有适当的读&写入权限.
  3. 我已经将此答案更新为也地址您的其他问题.
  1. For simplicity, I suggest using CURL instead of exec as it should be a lot faster and more reliable. This checks for the HTTP status code "200" which means it returned a valid request.
  2. You will need to make sure your text file/s have the appropriate read & write permissions.
  3. I've updated this answer to also address your other question.


此示例中的初始文本文件名为data.txt,其中包含以下内容:


The initial text file in this example is named data.txt and contains the following:

p1|google.com|
p2|yahoo.com|
p2|amazon.com|

以下代码将循环遍历列表中的每个服务器,并在在线时以最新日期更新记录.

The following code will cycle through each server in the list, and update the records with the latest date if it's online.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="30">
</head>
<body>

<h1>PC Test Ping Status</h1>

<?php

function ping($addr) {
    $ch = curl_init($addr);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //get response code
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($code === 200) {
        return true;
    }

    return false;
}

$file = 'data.txt';
$servers = array_filter(explode("\n", file_get_contents($file)));
foreach ($servers as $key => $server) {
    list($sname, $saddr, $suptime) = explode('|', $server);
    if (ping($saddr)) {
        echo "<p>$sname is online</p>";
        $date = new DateTime();
        $suptime = $date->format('Y-m-d H:i:s');
    } else {
        echo "<p>$sname is offline since: ";
        if (trim($suptime) !== '') {
            echo $suptime . '</p>';
        } else {
            echo 'unknown</p>';
        }
    }
    $servers[$key] = implode('|', array($sname, $saddr, $suptime)) . "\n";
}
file_put_contents($file, $servers);

?>


</body>
</html>

这篇关于需要使用PHP ping跟踪PC的停机时间并显示D:HH:MM的停机时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 06:49