PHP 从复杂字符串中选择文本
我正在从运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于接口的名称,这应该有效:
对于 RX 和 TX 字节:
您应该进行测试,以确保您拥有每个接口的所有信息:
当您拥有其中四个时,然后进行另一个循环(例如使用
继续
)。PS 请注意您在系统中使用的语言,因为使用西班牙语您会得到“Paquetes RX:”。可以肯定的是,为了使您的代码更加健壮,您应该尝试使
shell_exec
将环境变量LC_MESSAGES
设置为en_us.utf-8< /代码> 例如。
For the names of the interfaces, this should work:
For the RX and TX bytes:
And you should test in order to be sure you have all the information for every interface:
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 variableLC_MESSAGES
toen_us.utf-8
for example.