【问题标题】:Pinging an IP address using PHP and echoing the result使用 PHP ping IP 地址并回显结果
【发布时间】:2011-12-23 06:23:52
【问题描述】:

我有以下功能,到目前为止我还没有工作。我想 ping 一个 IP 地址,然后回显该 IP 是否存在。

function pingAddress($ip){
    $pingresult = shell_exec("start /b ping $ip -n 1");
    $dead = "Request timed out.";
    $deadoralive = strpos($dead, $pingresult);

    if ($deadoralive == false){
        echo "The IP address, $ip, is dead";
    } else {
        echo "The IP address, $ip, is alive";
    }

}

当我使用示例调用此函数时:

pingAddress("127.0.0.1")

回显结果总是“死”——无论如何。

有人可以帮我解决我哪里出错了吗? 和/或是否有更好的方法可以达到相同的结果?

非常感谢。

更新:已修改代码以包含双引号,但仍然得到相同(不正确)的结果。

【问题讨论】:

  • shell_exec 的调用需要使用双引号,否则$ip 将不会被替换。
  • 嗨 Clive,谢谢 - 我现在使用了双引号,但仍然得到相同的结果。

标签: php ping shell-exec


【解决方案1】:

注意:以下解决方案不适用于 Windows。在 linux 上,从控制台执行“which ping”命令,并相应地设置(建议的 exec 调用的)命令路径

我认为您想检查命令的退出状态,而 shell_exec 为您提供完整的输出(如果命令输出从命令版本更改为版本可能很危险。出于某种原因)。此外,您的变量 $ip 不会在单引号内解释。你必须使用双“”。这可能是您唯一需要解决的问题才能使其正常工作。

但我认为以下代码可以更“便携”。恕我直言,实际上捕获退出状态比尝试解析结果字符串更好。恕我直言,最好指定 ping 命令的完整路径。

<?php
function pingAddress($ip) {
    $pingresult = exec("/bin/ping -n 3 $ip", $outcome, $status);
    if (0 == $status) {
        $status = "alive";
    } else {
        $status = "dead";
    }
    echo "The IP address, $ip, is  ".$status;
}

pingAddress("127.0.0.1");

【讨论】:

  • HI maraspin,已经尝试过使用上面的这个功能,但是当我尝试它时,这会说即使 999.999.999.999 还活着 - 还有什么问题吗?您能否提出任何故障排除提示?
  • 1) ping 与大多数 UNIX 命令行实用程序一样,成功时返回 0,而不是 1。2) OP 正在使用Windows,所以/bin/ping 不起作用。
  • 删除 /bin/ 并将 0 更改为 1 看起来有效 - 谢谢大家。
  • 我建议用类似if(!filter_var($ip, FILTER_VALIDATE_IP) === false){的if语句包装exec
  • @Michele 尝试使用 -c 开关
【解决方案2】:

这对我在 Wordpress 中也不起作用。我也试过 -t 和 -n 等方法,但没有奏效。我用过,

function pingAddress($ip) {
    $pingresult = exec("/bin/ping -c2 -w2 $ip", $outcome, $status);  
    if ($status==0) {
    $status = "alive";
    } else {
    $status = "dead";
    }
    $message .= '<div id="dialog-block-left">';
    $message .= '<div id="ip-status">The IP address, '.$ip.', is  '.$status.'</div><div style="clear:both"></div>';    
    return $message;
}
// Some IP Address
pingAddress("192.168.1.1"); 

最后,这对我来说非常有效。 我从http://www.phpscriptsdaily.com/php/php-ping/ 提到了这个 希望这会有所帮助

好吧,我想修改它,因为它在我的本地主机上运行良好,但在我的实时服务器上却不行 对于实时服务器,我得到了另一件事,它现在既适用于本地服务器,也适用于实时服务器。

$fp = fSockOpen($ip,80,$errno,$errstr,1);
if($fp) { $status=0; fclose($fp); } else { $status=1; }

然后我显示服务器在 0 时启动,在 1 时停止。

这对我来说非常有效。我是从 Ping site and return result in PHP 那里得到的,谢谢 @karim79

【讨论】:

    【解决方案3】:

    我开发了适用于异构操作系统(Windows 和 Linux)的算法。

    实现以下类:

    <?php
    
        class CheckDevice {
    
            public function myOS(){
                if (strtoupper(substr(PHP_OS, 0, 3)) === (chr(87).chr(73).chr(78)))
                    return true;
    
                return false;
            }
    
            public function ping($ip_addr){
                if ($this->myOS()){
                    if (!exec("ping -n 1 -w 1 ".$ip_addr." 2>NUL > NUL && (echo 0) || (echo 1)"))
                        return true;
                } else {
                    if (!exec("ping -q -c1 ".$ip_addr." >/dev/null 2>&1 ; echo $?"))
                        return true;
                }
    
                return false;
            }
        }
    
        $ip_addr = "151.101.193.69"; #DNS: www.stackoverflow.com
    
        if ((new CheckDevice())->ping($ip_addr))
            echo "The device exists";
        else 
            echo "The device is not connected";
    

    【讨论】:

    • 请不要重新发布多个问题的类似答案。相反,针对所提出的问题定制答案。如果问题过于宽泛或相互重复,请改为使用版主。
    【解决方案4】:

    我刚刚通过结合以上所有知识增益编写了一个非常快速的解决方案

        function pinger($address){
            if(strtolower(PHP_OS)=='winnt'){
                $command = "ping -n 1 $address";
                exec($command, $output, $status);
            }else{
                $command = "ping -c 1 $address";
                exec($command, $output, $status);
            }
            if($status === 0){
                return true;
            }else{
                return false;
            }
        }
    

    【讨论】:

    • 我喜欢这个!我需要的很好的答案
    【解决方案5】:

    这对我来说很好..

    $host="127.0.0.1";
    $output=shell_exec('ping -n 1 '.$host);
    
    echo "<pre>$output</pre>"; //for viewing the ping result, if not need it just remove it
    
    if (strpos($output, 'out') !== false) {
        echo "Dead";
    }
        elseif(strpos($output, 'expired') !== false)
    {
        echo "Network Error";
    }
        elseif(strpos($output, 'data') !== false)
    {
        echo "Alive";
    }
    else
    {
        echo "Unknown Error";
    }
    

    【讨论】:

      【解决方案6】:

      对于 Windows 使用这个类

      $host = 'www.example.com';
      $ping = new Ping($host);
      $latency = $ping->ping();
      if ($latency !== false) {
       print 'Latency is ' . $latency . ' ms';
      }
      else {
      print 'Host could not be reached.';
      }
      

      https://github.com/geerlingguy/Ping

      【讨论】:

        【解决方案7】:

        这适用于主机名、反向 IP(用于内部网络)和 IP。

        function pingAddress($ip) {
            $ping = exec("ping -n 2 $ip", $output, $status);
            if (strpos($output[2], 'unreachable') !== FALSE) {
                return '<span style="color:#f00;">OFFLINE</span>';
            } else {
                return '<span style="color:green;">ONLINE</span>';
            }
        }
        
        echo pingAddress($ip);
        

        【讨论】:

        • 永远不要直接在exec 中使用用户输入。请参阅 this question 了解清理用户输入数据的正确方法。
        • 是的,没错。在我的代码中,我有一个围绕 $_get 的清理函数,但与这个问题有点无关,所以我删除了它。编辑:删除了 $_get 参数以便清楚。
        【解决方案8】:

        在尝试其中一些示例之前,请检查您的 ping 命令的手册页(无论如何总是好的做法)。对于 Ubuntu 16(例如),接受的答案不起作用,因为 -n 3 失败(这不再是数据包的计数,-n 表示不将 IP 地址转换为主机名)。

        根据 OP 的要求,可能的替代功能如下:

        function checkPing($ip){
            $ping = trim(`which ping`);
            $ll = exec($ping . '-n -c2 ' . $ip, $output, $retVar);
            if($retVar == 0){
                echo "The IP address, $ip, is alive";
                return true;
            } else {
                echo "The IP address, $ip, is dead";
                return false;
            }
        }
        

        【讨论】:

          【解决方案9】:

          我使用这个功能:

          <?php
          function is_ping_address($ip) {
              exec('ping -c1 -w1 '.$ip, $outcome, $status);
              preg_match('/([0-9]+)% packet loss/', $outcome[3], $arr);
              return ( $arr[1] == 100 ) ? false : true;
          }
          

          【讨论】:

            猜你喜欢
            • 2018-01-14
            • 1970-01-01
            • 2011-06-24
            • 1970-01-01
            • 1970-01-01
            • 2015-07-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多