php通过指令“ntpq -p”返回数据 html通过ajax调用php获取返回的数据 并绘制成表格输出
时间: 2024-03-05 19:48:25 浏览: 69
好的,您可以参考以下代码实现:
PHP代码(ntp.php):
```php
<?php
$output = shell_exec("ntpq -p");
echo $output;
?>
```
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ntp表格</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<table id="ntp_table" border="1">
<thead>
<tr>
<th>remote</th>
<th>refid</th>
<th>st</th>
<th>t</th>
<th>when</th>
<th>poll</th>
<th>reach</th>
<th>delay</th>
<th>offset</th>
<th>jitter</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
$(document).ready(function() {
$.ajax({
url: "ntp.php",
type: "GET",
success: function(data) {
var lines = data.split('\n');
for (var i = 2; i < lines.length-1; i++) {
var cells = lines[i].split(/\s+/);
var row = "<tr>";
for (var j = 0; j < cells.length; j++) {
row += "<td>" + cells[j] + "</td>";
}
row += "</tr>";
$("#ntp_table tbody").append(row);
}
}
});
});
</script>
</body>
</html>
```
这个代码会通过Ajax调用ntp.php脚本,获取“ntpq -p”命令的输出,并将其转换成表格输出。请注意,您需要在服务器上安装和配置ntp服务,以便在运行“ntpq -p”命令时返回输出。
阅读全文