ryangurn/ping

Ping 使用 ICMP 协议中强制性的 ECHO_REQUEST 数据报来从主机或网关获取 ICMP ECHO_RESPONSE。

dev-master 2023-03-04 21:43 UTC

This package is auto-updated.

Last update: 2024-09-04 02:49:00 UTC


README

License Latest Stable Version StyleCI Total Downloads

Quality Gate Status Coverage Maintainability Rating Security Rating

此 ping 类允许从 Laravel 应用程序中发送 ping 请求,它基于 Linux iputils 包中的 PING 命令。

ping 使用 ICMP 协议中强制性的 ECHO_REQUEST 数据报来从主机或网关获取 ICMP ECHO_RESPONSE。ECHO_REQUEST 数据报(ping)包含 IP 和 ICMP 头部,然后是一个 struct timeval,之后是任意数量的 padbytes,用于填充数据包。

安装

您可以通过 composer 安装此包,然后发布资产

在 Ping 2.1.0 版本之前,您可以使用以下方法安装:

composer require acamposm/ping

php artisan vendor:publish --provider="Acamposm\Ping\PingServiceProvider"

从 Ping 2.1.0 版本开始,您可以使用以下方法安装:

composer require acamposm/ping

php artisan ping:install

用法

对于基本用法,您只需要使用 IP 地址作为第一个参数创建它,然后运行...

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

// Create an instance of PingCommand
$command = (new PingCommandBuilder('192.168.1.1'));

// Pass the PingCommand instance to Ping and run...
$ping = (new Ping($command))->run();

更改计数

在发送指定数量的 ECHO_REQUEST 数据包后停止。使用 deadline 选项,ping 等待指定数量的 ECHO_REPLY 数据包,直到超时到期。

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

// Change the number of packets to send to 10
$command = (new PingCommandBuilder('192.168.1.1'))->count(10);

$ping = (new Ping($command))->run();

更改间隔

在发送每个数据包之间等待的秒数。默认情况下,每个数据包之间等待一秒钟,或者在 flood 模式下不等待。只有超级用户可以将间隔设置为小于 0.2 秒的值。

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

// Change interval to 0.5 seconds between each packet
$command = (new PingCommandBuilder('192.168.1.1'))->interval(0.5);

$ping = (new Ping($command))->run();

更改数据包大小

指定要发送的数据字节数。默认为 56,与 8 个字节的 ICMP 头数据结合时,相当于 64 个 ICMP 数据字节。

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

// Change packet size to 128
$command = (new PingCommandBuilder('192.168.1.1'))->packetSize(128);

$ping = (new Ping($command))->run();

更改超时

等待响应的时间,以秒为单位。此选项仅影响没有响应时的超时,否则 ping 等待两个往返时间。

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

// Change timeout to 10 seconds
$command = (new PingCommandBuilder('192.168.1.1'))->timeout(10);

$ping = (new Ping($command))->run();

更改生存时间

ping 仅。设置 IP 生存时间。

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

// Change Time To Live to 128
$command = (new PingCommandBuilder('192.168.1.1'))->ttl(128);

$ping = (new Ping($command))->run();

示例输出

在这里,您可以看到 ping 命令的三个输出示例...

  • 第一个使用域名。
  • 第二个使用 IPv4 地址
  • 第三个使用 IPv6 地址

在基于 Windows 的服务器上对 https://google.com 的示例输出

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

$command = (new PingCommandBuilder('https://google.com'))->count(10)->packetSize(200)->ttl(128);

// Sample output from Windows based server
$ping = (new Ping($command))->run();

dd($ping);
{
    "host_status": "Ok",
    "raw": [
        "",
        "Haciendo ping a google.com [216.58.213.142] con 200 bytes de datos:",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=37ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=37ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "",
        "Estadísticas de ping para 216.58.213.142:",
        "    Paquetes: enviados = 10, recibidos = 10, perdidos = 0",
        "    (0% perdidos),",
        "Tiempos aproximados de ida y vuelta en milisegundos:",
        "    Mínimo = 36ms, Máximo = 37ms, Media = 36ms",
    ],
    "latency": 0.036,
    "rtt": {
        "avg": 0.036,
        "min": 0.036,
        "max": 0.037,
    },
    "sequence": {
        "0": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "1": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "2": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "3": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=37ms TTL=115",
        "4": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=37ms TTL=115",
        "5": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "6": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "7": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "8": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
        "9": "Respuesta desde 216.58.213.142: bytes=68 (enviados 200) tiempo=36ms TTL=115",
    },
    "statistics": {
        "packets_transmitted": 10,
        "packets_received": 10,
        "packets_lost": 0,
        "packet_loss": 0,
    },
    "options": {
        "host": "google.com",
        "count": 10,
        "packet_size": 200,
        "ttl": 120,
    },
    "time": {
        "start": {
            "as_float": 1596984650.5006,
            "human_readable": "09-08-2020 14:50:50.500600",
        },
        "stop": {
            "as_float": 1596984659.5802,
            "human_readable": "09-08-2020 14:50:59.580200",
        },
        "time": 9.08,
    },
}

从基于 Windows 的服务器到本地网关 IPv4 的示例输出

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

$command = (new PingCommandBuilder('192.168.10.254'))->count(10)->packetSize(200)->ttl(120);

$ping = (new Ping($command))->run();

dd($ping);
{
    "host_status": "Ok",
    "raw": [
        "",
        "Haciendo ping a 192.168.10.254 con 200 bytes de datos:",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "",
        "Estadísticas de ping para 192.168.10.254:",
        "    Paquetes: enviados = 10, recibidos = 10, perdidos = 0",
        "    (0% perdidos),",
        "Tiempos aproximados de ida y vuelta en milisegundos:",
        "    Mínimo = 0ms, Máximo = 0ms, Media = 0ms",
    ],
    "latency": 0.0,
    "rtt": {
        "avg": 0.0,
        "min": 0.0,
        "max": 0.0,
    },
    "sequence": {
        "0": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "1": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "2": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "3": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "4": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "5": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "6": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "7": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "8": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
        "9": "Respuesta desde 192.168.10.254: bytes=200 tiempo<1m TTL=255",
    },
    "statistics": {
        "packets_transmitted": 10,
        "packets_received": 10,
        "packets_lost": 0,
        "packet_loss": 0,
    },
    "options": {
        "host": "192.168.10.254",
        "count": 10,
        "packet_size": 200,
        "ttl": 120,
        "version": 4,
    },
    "time": {
    "start": {
        "as_float": 1596985359.7592,
        "human_readable": "09-08-2020 15:02:39.759200",
    },
    "stop": {
        "as_float": 1596985368.7952,
        "human_readable": "09-08-2020 15:02:48.795200",
    },
    "time": 9.036,
    },
}

从基于 Windows 的服务器到链路本地 IPv6 地址的示例输出

use Acamposm\Ping\Ping;
use Acamposm\Ping\PingCommandBuilder;

$command = (new PingCommandBuilder('fe80::6c42:407d:af01:9567'))->count(10)->packetSize(200)->ttl(120);

$ping = (new Ping($command))->run();

dd($ping);
{
    "host_status": "Ok",
    "raw": [
        "",
        "Haciendo ping a fe80::6c42:407d:af01:9567 con 200 bytes de datos:",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "",
        "Estadísticas de ping para fe80::6c42:407d:af01:9567:",
        "    Paquetes: enviados = 10, recibidos = 10, perdidos = 0",
        "    (0% perdidos),",
        "Tiempos aproximados de ida y vuelta en milisegundos:",
        "    Mínimo = 0ms, Máximo = 0ms, Media = 0ms",
    ],
    "latency": 0.0,
    "rtt": {
        "avg": 0.0,
        "min": 0.0,
        "max": 0.0,
    },
    "sequence": {
        "0": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "1": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "2": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "3": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "4": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "5": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "6": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "7": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "8": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
        "9": "Respuesta desde fe80::6c42:407d:af01:9567: tiempo<1m",
    },
    "statistics": {
        "packets_transmitted": 10,
        "packets_received": 10,
        "packets_lost": 0,
        "packet_loss": 0,
    },
    "options": {
        "host": "fe80::6c42:407d:af01:9567",
        "count": 10,
        "packet_size": 200,
        "ttl": 120,
        "version": 6,
    },
    "time": {
        "start": {
            "as_float": 1596985675.4344,
            "human_readable": "09-08-2020 15:07:55.434400",
        },
        "stop": {
            "as_float": 1596985684.4659,
            "human_readable": "09-08-2020 15:08:04.465900",
        },
        "time": 9.032,
    },
}

测试

要运行测试,请在控制台中运行

composer test

变更日志

请参阅 CHANGELOG 了解最近更改了什么。

贡献

感谢您考虑为此包的改进做出贡献。请参阅 CONTRIBUTING 了解详细信息。

安全 & 漏洞

如果您发现任何安全相关的问题,请通过 angel.campos.m@outlook.com 发送电子邮件给 Angel Campos,而不是使用问题跟踪器。所有安全漏洞都将得到及时解决。

标准

PHP 包 IPv4 地址转换器符合以下标准

致谢

许可

此包 Ping 是开源包,并采用 MIT 许可证(MIT)许可。请参阅 许可文件 了解更多信息。