简体   繁体   English

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

[英]Exec ping command doesn't work on Linux Centos

Good day, 美好的一天,

I am working on a system which checks whether a certain IP address is up or down. 我正在使用一个系统来检查某个IP地址是打开还是关闭。 I have a MySQL DB with a table containing a list of IP addresses.I have created the following function to ping the IP address: 我有一个MySQL DB,其数据库表包含IP地址列表。我创建了以下函数来ping IP地址:

function pingAddress($ip) {

        $result = exec('ping $ip -i 15 ', $outcome, $status);

        if (0 == $status) {
            echo 'Server UP ';
        } else {
            echo 'Server down ';
        }
}

and in my while loop I have the following: 在我的while循环中,我有以下内容:

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
   ?>
   <tr>
       <td><?php pingAddress($row['client_ip_address']); ?></td>
   </tr>

   <?php
   } //end while
  ?>

However instead of getting 0 (because servers are up) I always get 2. 但是,我总是得到2,而不是得到0(因为服务器已启动)。

I have replaced the line $result = exec('ping $ip -i 15 ', $outcome, $status); 我已经替换了行$result = exec('ping $ip -i 15 ', $outcome, $status); with $pingresult = system("ping -n 3 $ip 2>&1", $outcome, $status); $pingresult = system("ping -n 3 $ip 2>&1", $outcome, $status); OR $pingresult = shell_exec('/bin/ping -qc 1 '.$ip.' | awk -F/ \\'/^rtt/\\'', $result); OR ('ping $ip -i 15 ', $outcome, $status); $pingresult = shell_exec('/bin/ping -qc 1 '.$ip.' | awk -F/ \\'/^rtt/\\'', $result); OR ('ping $ip -i 15 ', $outcome, $status); $pingresult = shell_exec('/bin/ping -qc 1 '.$ip.' | awk -F/ \\'/^rtt/\\'', $result); OR ('ping $ip -i 15 ', $outcome, $status); Still the result didn't change. 结果仍然没有改变。 Either I got an empty array (when I do var_dump($status) or var_dump($outcome) ). 我得到一个空数组(执行var_dump($status)var_dump($outcome) )。

I am using Centos7 with apache installed on it. 我正在使用安装了Apache的Centos7。

I am a missing something because I have tried virtually the majority of solutions suggested here based on previous similar questions but it didn't work. 我缺少了一些东西,因为我根据以前的类似问题尝试了此处建议的大多数解决方案,但是没有用。

Is there a much better way code such a system? 有没有更好的方法来编码这样的系统?

Thanks for your assistance. 谢谢你的协助。

You likely need to modify the syntax of exec : 您可能需要修改exec的语法:

$result = exec('ping -i 15 -c 3 ' . $ip, $outcome, $status);

Also, you might include -c or whatever your ping count option is... otherwise it will run forever. 另外,您可以包括-c或任何ping count选项...否则它将永久运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM