php - 执行 ping 命令在 Linux Centos 上不起作用

标签 php linux centos exec ping

你好,

我正在开发一个系统,该系统检查某个 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/

相关文章:

linux - 跟踪命令在 UNIX/LINUX 中花费的时间?

apache - 如何在 CentOS 上使用 Tomcat 设置 Apache

docker : can't mount some directory from host

php - 文件存在于/tmp,但 PHP 在 CentOS 上返回 "No such file or directory"

php - 使用 Apache Benchmark 测试 PHP 项目的性能?

php - 查看MySQL中6个表的数据

linux - 如何删除用户的 ssh keygen?

linux - 无法为 barnyard2 运行配置

php - .htaccess 不加载 css 和 js

php - 每年更新数据库表