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

时间:2017-09-05 09:31:42

标签: php

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

以下代码适用于Windows。但是没有对Raspberry 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 
        { 
        }
   }

2 个答案:

答案 0 :(得分:0)

你的Pi可能正在运行* nix而ping有一组略有不同的标志。

我假设你使用-n只说1个ping,在* nix它&#39; s -c。您可能会错过此参数,只需使用-w来限制ping。所以代码是......

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

答案 1 :(得分:0)

在Raspberry Pi零W和PHP7上,我使用(用于板载wifi适配器):

<?xml version="1.0" encoding="utf-8"?>
<TestProfile xmlns="http://www.specflow.org/schemas/plus/TestProfile/1.5">
  <Settings projectName="PLACEHOLDER_NOTREAL" reportTemplate=".\\..\\..\\..\\RunSettings\\JacksSampleTemplate.cshtml"/>
  <!-- Look at this website for execution configurations : "https://github.com/SpecFlowOSS/SpecFlowPlus-Resources/wiki/Execution"-->
  <Execution stopAfterFailures="3" testThreadCount="1" testSchedulingMode="Sequential" retryCount="0" />
  <!-- For collecting by a SpecRun server update and enable the following element. For using the 
      collected statistics, set testSchedulingMode="Adaptive" attribute on the <Execution> element.
    <Server serverUrl="http://specrunserver:6365" publishResults="true" />
  -->
  <TestAssemblyPaths>
    <TestAssemblyPath>FAKE.dll</TestAssemblyPath>
  </TestAssemblyPaths>
  <DeploymentTransformation>
    <Steps>
      <!-- sample config transform to change the connection string-->
      <!--<ConfigFileTransformation configFile="App.config">
        <Transformation>
          <![CDATA[<?xml version="1.0" encoding="utf-8"?>
                            <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
                <connectionStrings>
                  <add name="MyDatabase" connectionString="Data Source=.;Initial Catalog=MyDatabaseForTesting;Integrated Security=True" 
                       xdt:Locator="Match(name)" xdt:Transform="SetAttributes(connectionString)" />
                </connectionStrings>
                            </configuration>
                        ]]>
        </Transformation>
      </ConfigFileTransformation>-->
    </Steps>
  </DeploymentTransformation>
</TestProfile>