PHP:如何更快地ping一个IP地址

PHP:如何更快地ping一个IP地址,php,networking,server,monitoring,ping,Php,Networking,Server,Monitoring,Ping,我正在使用exec(“ping”。$server->ip,$output,$result);函数,我正在尝试ping多个ip地址,一个接一个地ping它们需要太多时间。有没有更快的方法来ping它们 //In my controller $servers = Server::orderBy('created_at', 'desc')->paginate(10); foreach ($servers as $server) { exec("p

我正在使用exec(“ping”。$server->ip,$output,$result);函数,我正在尝试ping多个ip地址,一个接一个地ping它们需要太多时间。有没有更快的方法来ping它们

    //In my controller
    $servers = Server::orderBy('created_at', 'desc')->paginate(10);

    foreach ($servers as $server)
    {
        exec("ping " . $server->ip, $output, $result);

        if($result != 0)
        {
          $server->status = 0;
        }
        else
        {
          $server->status = 1;
        }

    }

这里有两种方法:

  • 使用批处理文件
  • 使用
    fsockopen()
    函数
  • 1.使用批处理文件: 为什么要麻烦使用它呢?因为每次调用
    exec
    函数时,大部分时间都花在创建shell上,所以我们的想法是创建一个小程序,只需调用一次
    system
    PHP命令,就可以在命令行上循环访问您的IP地址,下面是你需要做的事情和之后的解释。 -在doucment\u根目录中创建一个批处理文件
    batch.bat
    ,并将以下命令插入其中:

    set list=172.30.240.56 172.30.240.57 172.30.240.58
    (for %%a in (%list%) do ( 
       ping -n 1 %%a 
    ));
    
    您可以通过用空格分隔的IP地址填写上面的列表。 ping-n1是只ping一次,因为你需要速度

    Then in your script, it will be as simple as putting:
    echo '<pre>';
    exec('cmd /c .\batch.bat',$result);
    /* $result is an array of lines of the output that you can easily access
    look at the result obtained by using print_r($result); below
    
    我已经用
    www.google.com
    172.30.240.56
    对其进行了测试,结果如下: (请注意,对于第二个IP地址,ping失败)

    2.使用
    fsockopen
    命令: 使用套接字,它是内置的PHP,因此维护和检测错误更快、更容易,下面是ping Ip地址的示例代码

    $fp=fsockopen(“www.google.com”,80,$errno,$errstr,30);
    如果(!$fp){
    回显“$errstr($errno)
    \n”; }否则{ $out=“GET/HTTP/1.1\r\n”; $out.=“主机:www.google.com\r\n”; $out.=“连接:关闭\r\n\r\n”; fwrite($fp,$out); 而(!feof($fp)){ echo fgets(fp,128美元); } fclose($fp); }
    所以这里有两种方法:

  • 使用批处理文件
  • 使用
    fsockopen()
    函数
  • 1.使用批处理文件: 为什么要麻烦使用它呢?因为每次调用
    exec
    函数时,大部分时间都花在创建shell上,所以我们的想法是创建一个小程序,只需调用一次
    system
    PHP命令,就可以在命令行上循环访问您的IP地址,下面是你需要做的事情和之后的解释。 -在doucment\u根目录中创建一个批处理文件
    batch.bat
    ,并将以下命令插入其中:

    set list=172.30.240.56 172.30.240.57 172.30.240.58
    (for %%a in (%list%) do ( 
       ping -n 1 %%a 
    ));
    
    您可以通过用空格分隔的IP地址填写上面的列表。 ping-n1
    是只ping一次,因为你需要速度

    Then in your script, it will be as simple as putting:
    echo '<pre>';
    exec('cmd /c .\batch.bat',$result);
    /* $result is an array of lines of the output that you can easily access
    look at the result obtained by using print_r($result); below
    
    我已经用
    www.google.com
    172.30.240.56
    对其进行了测试,结果如下: (请注意,对于第二个IP地址,ping失败)

    2.使用
    fsockopen
    命令: 使用套接字,它是内置的PHP,因此维护和检测错误更快、更容易,下面是ping Ip地址的示例代码

    $fp=fsockopen(“www.google.com”,80,$errno,$errstr,30);
    如果(!$fp){
    回显“$errstr($errno)
    \n”; }否则{ $out=“GET/HTTP/1.1\r\n”; $out.=“主机:www.google.com\r\n”; $out.=“连接:关闭\r\n\r\n”; fwrite($fp,$out); 而(!feof($fp)){ echo fgets(fp,128美元); } fclose($fp); }
    我尝试使用fsock功能,但我以前的一些在线ip地址变得无效offline@GrantGubatan,您是否检查以确保端口未被防火墙/iptableshow阻塞?我应该在哪里检查它?@GrantGubatan,使用端口80尝试默认示例,以查看它是否可能是端口阻塞issue@GrantGubatan,我已经更新了代码,有一个测试过的代码运行得非常好,请尝试一下这个完全相同的代码,并告诉我发生了什么我尝试使用fsock函数,但我以前的一些在线ip地址变了offline@GrantGubatan,您是否检查以确保端口未被防火墙/iptableshow阻止,我应该在哪里检查它?@GrantGubatan,尝试使用端口80的默认示例,看看它是否可能是端口块issue@GrantGubatan,我已经更新了代码,使用了一个运行良好的测试代码,请尝试这个完全相同的代码,并告诉我发生了什么可能重复的可能重复的
    $fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
    } else {
        $out = "GET / HTTP/1.1\r\n";
        $out .= "Host: www.google.com\r\n";
        $out .= "Connection: Close\r\n\r\n";
        fwrite($fp, $out);
        while (!feof($fp)) {
            echo fgets($fp, 128);
        }
        fclose($fp);
    }