过滤掉PHP shell_exec输出结果

时间:2020-10-27 15:44:18

标签: php linux raspberry-pi shell-exec

我正在尝试创建一种使用php在我的网络上找到Raspberry pi的方法,我想知道如何过滤此结果,以便只能看到一台特定计算机的IP地址?

<?php
$output = shell_exec('sudo arp-scan --localnet');
echo "<pre>$output</pre>";
?>

这是执行php代码时的输出示例。

Interface: wlan0, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.9.5 with 256 hosts (https://github.com/royhills/arp-scan)

192.168.1.62    b8:29:0b:ed:e0:27   Raspberry Pi Foundation
192.168.1.95    14:6b:00:00:ec:59   (Unknown)
192.168.1.72    24:46:11:00:ee:3f   (Unknown)


15 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.9.5: 256 hosts scanned in 3.722 seconds (68.78 hosts/sec). 15 responded

我正在尝试寻找一种过滤所有文本的方法,但在这种情况下将Raspberry Pi Foundation的IP地址保留为192.168.1.62

2 个答案:

答案 0 :(得分:0)

preg_match_all最适合此类问题

<?php
$text = 'Interface: wlan0, datalink type: EN10MB (Ethernet)
Starting arp-scan 1.9.5 with 256 hosts (https://github.com/royhills/arp-scan)

192.168.1.62    b8:29:0b:ed:e0:27   Raspberry Pi Foundation
192.168.1.95    14:6b:00:00:ec:59   (Unknown)
192.168.1.72    24:46:11:00:ee:3f   (Unknown)


15 packets received by filter, 0 packets dropped by kernel
Ending arp-scan 1.9.5: 256 hosts scanned in 3.722 seconds (68.78 hosts/sec). 15 responded
';
$regMatch = '/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\ \ \ (.*)\ \ \ (.*)/';
preg_match_all( $regMatch, $text, $out, PREG_PATTERN_ORDER);
print_r($out);

print_r($out[2]);  #vendor array

答案 1 :(得分:0)

更改命令

sudo arp-scan --localnet 

收件人:

"sudo arp-scan --localnet | awk '/Raspberry Pi Foundation/ { print $1 }'"