本文介绍了PHP修剪nmap MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Windows 服务器上运行脚本以使用 nmap 查找设备 MAC 地址.

I am running a script on a windows server to find device MAC address with nmap.

$ip= $_SERVER['REMOTE_ADDR'];

$line = "C:\\nmap -sP -n $ip";

echo "You IP address ";
echo $ip;
echo "<br><br>";

$ping = shell_exec("$line");

$mac = substr($ping,156,17);

echo "MAC ADDRESS: ";
echo $mac;

每次运行脚本时,MAC 地址输出都会略有不同.我怀疑是因为该命令添加了时间和延迟信息,从而改变了字符数.有没有更有效的方法只提取mac地址?

The MAC address output varies a little each time the scripted is run. I suspect it because the command adds time and latency information which in turn changed the character count. Is there a more effective way to pull just the mac address?

(nmap -sP -n $ip 的原始示例输出)

Starting Nmap 6.46 ( http://nmap.org ) at 2014-07-29 10:00 Central Daylight Time
Nmap scan report for 10.0.0.152
Host is up (0.00s latency).
MAC Address: C8:F6:50:FF:FF:FF (Apple)
Nmap done: 1 IP address (1 host up) scanned in 0.39 seconds

推荐答案

您可以使用多个字符串函数或正则表达式来提取 MAC 地址:

You can either use several string functions or a regular expression to pull out the MAC address:

字符串方法:

$macLookup = 'MAC Address: ';
$pos = strpos($ping, $macLookup);

if ($pos !== false) {
    $mac = substr($ping, $pos+strlen($macLookup), 17 );
    echo $mac; //C8:F6:50:FF:FF:FF
}

正则表达式方法:

if (preg_match('/MAC Address: ([A-F0-9:]+)/', $ping, $matches)) {
     $mac = $matches[1]; //C8:F6:50:FF:FF:FF
}

在线演示 此处.

这篇关于PHP修剪nmap MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 05:49