你好,
我正在开发一个系统,该系统检查某个 IP 地址是启动还是关闭。我有一个 MySQL 数据库,其中包含一个包含 IP 地址列表的表。我创建了以下函数来 ping IP 地址:
function pingAddress($ip) {
$result = exec('ping $ip -i 15 ', $outcome, $status);
if (0 == $status) {
echo 'Server UP ';
} else {
echo 'Server down ';
}
}
在我的 while 循环中,我有以下内容:
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
?>
<tr>
<td><?php pingAddress($row['client_ip_address']); ?></td>
</tr>
<?php
} //end while
?>
然而,我总是得到 2,而不是 0(因为服务器已启动)。
我已经替换了行 $result = exec('ping $ip -i 15 ', $outcome, $status);
使用 $pingresult = system("ping -n 3 $ip 2>&1", $outcome, $status);
或 $pingresult = shell_exec('/bin/ping -qc 1 '.$ip.' | awk -F/\'/^rtt/\'', $result);或 ('ping $ip -i 15 ', $outcome, $status);
结果还是没有改变。我得到一个空数组(当我执行 var_dump($status)
或 var_dump($outcome)
时)。
我正在使用安装了 apache 的 Centos7。
我遗漏了一些东西,因为我已经尝试了这里基于之前类似问题提出的几乎大多数解决方案,但没有奏效。
有没有更好的方法编写这样的系统?
感谢您的协助。
最佳答案
您可能需要修改 exec
的语法:
$result = exec('ping -i 15 -c 3 ' . $ip, $outcome, $status);
此外,您可以包括 -c
或任何您的 ping count
选项...否则它将永远运行。
关于php - 执行 ping 命令在 Linux Centos 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396160/