使用PHP代码查找局域网IP和MAC地址



使用PHP代码找到局部网络IP和MAC地址。

以下代码在Windows上工作。但没有在覆盆子Pi上工作。请帮我。

$ipadd = gethostbyname(trim(`hostname`));
    echo $ipadd."<br>";
    $add = explode(".",$ipadd);
    array_pop($add);
    $addre = implode(".",$add);
    $address = $addre.".";
    $i = 1;
    while($i < 254) 
    {
        pingAddress($address.$i,$ipadd);
        $i++;
    }
    function pingAddress($ip,$dip) 
    {
        $pingresult = exec("ping -n 1 -w 1 $ip", $outcome, $status);
        if (0 == $status) 
        {
            if ($ip != $dip) 
            {
                $cmd = "arp -a " . $ip;
                $macadd = exec($cmd);
                $str = $macadd;
                $mac = explode(" ",$str);
                echo $ip." - ".$mac[11]."<br>"; 
            }
            else
            {
                $string = exec('getmac');
                $mac = substr($string, 0, 17); 
                echo $ip." - ".$mac."<br>";
            }
        } 
        else 
        { 
        }
   }

您的pi可能正在运行 *nix,而ping的标志略有不同。

我假设您使用-n只说1 ping,在 *nix上是-c。您可能会错过此参数,然后使用-w来限制ping。因此代码是...

$pingresult = exec("ping -w 1 $ip", $outcome, $status);

在Raspberry Pi Zero W和PHP7上,我使用(用于板载WiFi Adaptator):

//get mac address
$mac=file_get_contents("/sys/class/net/wlan0/address");
echo "mac=".$mac;

最新更新