PHP 从复杂字符串中选择文本

发布于 2024-12-13 16:40:37 字数 1655 浏览 0 评论 0原文

我正在从运行 ifconfig 命令的 Linux 机器中获取输出。我通过运行 shell_exec 命令来执行此操作。我找到了一个例子,但我似乎无法进一步使用它。

这是我当前的代码。

public function get_network_interfaces() {

    // Run the command.
    $cmd_exec = shell_exec("/sbin/ifconfig");

    // Get the pattern.
    $pattern = '/inet addr:([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})/';

    if (preg_match_all($pattern, $cmd_exec, $matches)) {
        $interfaces = $matches[1];
        return $interfaces;         
    }

}

这个的输出(foreach'ing之后)很简单; 192.168.1.1/127.0.0.1 因为它有两个网络适配器,所以 ifconfig 的输出是;

eth0      Link encap:Ethernet  HWaddr 00:16:3e:38:b8:1c  
      inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: 2a02:2308::216:3eff:fe38:b81c/64 Scope:Global
      inet6 addr: fe80::216:3eff:fe38:b81c/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:10612623 errors:0 dropped:0 overruns:0 frame:0
      TX packets:151855 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:539434883 (514.4 MiB)  TX bytes:37861938 (36.1 MiB)
      Interrupt:30
lo        Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:6662 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6662 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:768641 (750.6 KiB)  TX bytes:768641 (750.6 KiB)

我想要得到的是,接口的名称(eth0 和 lo),以及 inet 添加(已经在工作),后面是 RX 字节和 TX 字节。 如果名字太难取,那就这样吧。但谁能解释一下如何获取 RX 字节和 TX 字节?

谢谢。

I am getting output from my linux box running the ifconfig command. I do so by running the shell_exec command. I found an example but I can't seem to work further with it.

This is my current code.

public function get_network_interfaces() {

    // Run the command.
    $cmd_exec = shell_exec("/sbin/ifconfig");

    // Get the pattern.
    $pattern = '/inet addr:([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})/';

    if (preg_match_all($pattern, $cmd_exec, $matches)) {
        $interfaces = $matches[1];
        return $interfaces;         
    }

}

The output of this (after foreach'ing) is simply; 192.168.1.1/127.0.0.1 because it has two network adapters, the output of ifconfig is;

eth0      Link encap:Ethernet  HWaddr 00:16:3e:38:b8:1c  
      inet addr:192.168.1.1  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: 2a02:2308::216:3eff:fe38:b81c/64 Scope:Global
      inet6 addr: fe80::216:3eff:fe38:b81c/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:10612623 errors:0 dropped:0 overruns:0 frame:0
      TX packets:151855 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:539434883 (514.4 MiB)  TX bytes:37861938 (36.1 MiB)
      Interrupt:30
lo        Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING  MTU:16436  Metric:1
      RX packets:6662 errors:0 dropped:0 overruns:0 frame:0
      TX packets:6662 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:768641 (750.6 KiB)  TX bytes:768641 (750.6 KiB)

What I am trying to get is like, the names of the interfaces (eth0 and lo), along with the inet adds (which is working already) followed by RX bytes and TX bytes.
If the name is too hard to get, so be it. But can anyone explain me how to get the RX bytes and TX bytes?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-12-20 16:40:37

对于接口的名称,这应该有效:

$pattern = '/^(\w*)\s*Link encap.*$/';

对于 RX 和 TX 字节:

$pattern = '/RX packets:(\d*) /';
$pattern = '/TX packets:(\d*) /';

您应该进行测试,以确保您拥有每个接口的所有信息:

  1. 测试接口的名称。
  2. 测试 IP 号码。
  3. 测试 RX 字节。
  4. 测试 TX 字节。

当您拥有其中四个时,然后进行另一个循环(例如使用继续)。

PS 请注意您在系统中使用的语言,因为使用西班牙语您会得到“Paquetes RX:”。可以肯定的是,为了使您的代码更加健壮,您应该尝试使 shell_exec 将环境变量 LC_MESSAGES 设置为 en_us.utf-8< /代码> 例如。

For the names of the interfaces, this should work:

$pattern = '/^(\w*)\s*Link encap.*$/';

For the RX and TX bytes:

$pattern = '/RX packets:(\d*) /';
$pattern = '/TX packets:(\d*) /';

And you should test in order to be sure you have all the information for every interface:

  1. Test for the name of the interface.
  2. Test for the IP number.
  3. Test for the RX byte.
  4. Test for the TX byte.

and when you have the four of them, then go for another cycle (for example with continue).

P.S. Be careful about the language you're using in your system, because with spanish you get "Paquetes RX:". Just to be sure, and in order to make your code more robust, you should try to make shell_exec to set environment variable LC_MESSAGES to en_us.utf-8 for example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文