21

我有以下功能,到目前为止我还没有工作。我想 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")

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

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

非常感谢。

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

4

9 回答 9

18

注意:以下解决方案不适用于 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");
于 2011-11-06T22:20:13.480 回答
8

这对我在 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 站点得到了这个并以 PHP 的形式返回结果谢谢@karim79

于 2012-05-11T12:16:22.850 回答
5

我已经开发了适用于异构操作系统的算法,包括 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";
于 2018-03-28T22:02:07.867 回答
3

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

    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;
        }
    }
于 2019-06-20T12:45:17.250 回答
2

对于 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

于 2017-06-16T11:09:12.307 回答
2

这对我来说很好..

$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";
}
于 2017-04-19T01:22:34.467 回答
0

这适用于主机名、反向 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);
于 2017-03-22T12:03:45.510 回答
0

Do check the man pages of your ping command before trying some of these examples out (always good practice anyway). For Ubuntu 16 (for example) the accepted answer doesn't work as the -n 3 fails (this isn't the count of packets anymore, -n denotes not converting the IP address to a hostname).

Following the request of the OP, a potential alternative function would be as follows:

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;
    }
}
于 2017-11-20T18:16:09.580 回答
-1

我使用这个功能:

<?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;
}
于 2017-09-19T08:18:22.240 回答