繁体   English   中英

找到连接到同一Wifi网络的所有设备的MAC地址

[英]Find the MAC address of all devices connected to the same Wifi network

我正在尝试构建一个软件来检查哪些设备连接到我的家庭网络,并每10分钟左右返回一个设备的MAC地址列表。

我的方法是ping网络上所有可能的IP地址,然后调用“arp -a”。

以下代码用于查找设备是否在IP地址上注册,但我不知道如何从中获取MAC地址。

try {
            String currentIP = InetAddress.getLocalHost().toString();
            String subnet = getSubnet(currentIP);
            System.out.println("subnet: " + subnet);

            for (int i=1;i<254;i++){

                String host = subnet + i;
                System.out.println("Checking :" + host);

                if (InetAddress.getByName(host).isReachable(timeout)){
                    System.out.println(host + " is reachable");
                    try {
                        Socket connected = new Socket(subnet, port);
                    }
                    catch (Exception s) {
                        System.out.println(s);
                    }
                }
            }
        }
        catch(Exception e){
            System.out.println(e);
        }

有什么建议么?

试试这一个

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class App{

   public static void main(String[] args){

    InetAddress ip;
    try {

        ip = InetAddress.getLocalHost();
        System.out.println("Current IP address : " + ip.getHostAddress());

        NetworkInterface network = NetworkInterface.getByInetAddress(ip);

        byte[] mac = network.getHardwareAddress();

        System.out.print("Current MAC address : ");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
        }
        System.out.println(sb.toString());

    } catch (UnknownHostException e) {

        e.printStackTrace();

    } catch (SocketException e){

        e.printStackTrace();

    }

   }

}

NetworkInterfaceNetworkInterface.getHardwareAddress()方法仅允许访问localhost MAC地址,而不是远程主机MAC地址。

注意:你无法在java中远程访问PC或设备。但是你可以使用C#和WMI - WMI与.NET语言中的netowrk一起使用

你盲目地假设IPV4,这些日子已经不再那么合理了。

而且你正在试图挖掘出路由器和接入点没有充分理由首先披露的信息(至少不是那些不会将自己认证为具有路由器或接入点管理页面访问权限的管理员的机器人) )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM