PHP:如何更快地ping IP地址

时间:2017-08-07 08:02:19

标签: 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("ping " . $server->ip, $output, $result);

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

    }

1 个答案:

答案 0 :(得分:2)

所以这里有两种方法:

  1. 使用批处理文件
  2. 使用fsockopen()函数
  3. 1。使用批处理文件:

    为什么要使用它,因为每次调用exec函数时,大部分时间都用于创建shell,所以我们的想法是创建一个迷你程序,在你的IP地址上循环命令行,只调用system PHP命令一次,所以这里你需要做什么以及之后的解释。     - 在doucment_root目录中创建批处理文件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 
    ));
    

    您可以通过由white_spaces分隔的IP地址填写上面的列表。 ping -n 1只需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
    

    您甚至可以在PHP脚本中自动创建批处理文件,前提是您拥有正确的权限(因为您可以运行exec,因此可以运行$servers = Server::orderBy('created_at', 'desc')->paginate(10); $batch_string=' set list='; foreach ($servers as $server) $batch_string.=$server->ip.' '; $batch_string.= "\n (for %%a in (%list%) do ( ping -n 1 %%a ));"; file_put_contents('batch.bat',$batch_string); echo '<pre>'; exec('cmd /c .\batch.bat',$result); ) :

    www.google.com

    我已使用172.30.240.56Array ( [0] => [1] => C:\Batch_File_path>set list=www.google.com 172.30.240.56 [2] => [3] => C:\Batch_File_path>(for %a in (www.google.com 172.30.240.56) do (ping -n 1 %a ) ) [4] => [5] => C:\Batch_File_path>(ping -n 1 www.google.com ) [6] => [7] => Pinging www.google.com [172.217.23.196] with 32 bytes of data: [8] => Reply from 172.217.23.196: bytes=32 time=84ms TTL=48 [9] => [10] => Ping statistics for 172.217.23.196: [11] => Packets: Sent = 1, Received = 1, Lost = 0 (0% loss), [12] => Approximate round trip times in milli-seconds: [13] => Minimum = 84ms, Maximum = 84ms, Average = 84ms [14] => [15] => C:\Batch_File_path>(ping -n 1 172.30.240.56 ) [16] => [17] => Pinging 172.30.240.56 with 32 bytes of data: [18] => Request timed out. [19] => [20] => Ping statistics for 172.30.240.56: [21] => Packets: Sent = 1, Received = 0, Lost = 1 (100% loss), ) 对此进行了测试,并且我得到了以下结果: (请注意,对于第二个IP地址,ping失败)

    fsockopen

    2。使用$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); } 命令:

    使用套接字,它是一个内置的PHP,因此它更快,更容易维护和检测错误,这是一个用于ping Ip地址的示例代码

    std::transform(input_image.begin(), input_image.end(), input_image.begin(), [lo](double v) {return (v < lo ? lo : v);});
    std::transform(input_image.begin(), input_image.end(), input_image.begin(), [hi](double v) {return (v > hi ? hi : v);});