【问题标题】:Find the MAC address of all devices connected to the same Wifi network查找连接到同一个 Wifi 网络的所有设备的 MAC 地址
【发布时间】:2015-10-30 11:14:37
【问题描述】:

我正在尝试构建一个软件来检查哪些设备连接到我的家庭网络,并每 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);
        }

有什么建议吗?

【问题讨论】:

    标签: java ping router mac-address arp


    【解决方案1】:

    试试这个one

    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()方法只允许访问本地主机MAC地址,不能访问远程主机MAC地址。

    注意:您不能在 Java 中远程为 PC 或设备执行此操作。但您可以使用 C# 和 WMI -WMI with netowrk in .NET language

    【讨论】:

    • -1 这不是解决“我正在尝试构建一个软件来检查哪些设备连接到我的家庭网络并返回这些设备的 MAC 地址列表”问题的解决方案。跨度>
    【解决方案2】:

    你在盲目地假设 IPV4,现在这不再那么合理了。

    而您首先试图挖掘出路由器和接入点没有充分理由披露的信息(至少对于那些不会通过路由器或接入点管理页面的访问权限)。

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多