简体   繁体   English

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

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

I'm trying to build a software that checks what devices are connected to my home network and return a list of those device's MAC address every 10 minutes or so. 我正在尝试构建一个软件来检查哪些设备连接到我的家庭网络,并每10分钟左右返回一个设备的MAC地址列表。

My approach was to ping all of the possible IP addresses on the network and call "arp -a" afterwards. 我的方法是ping网络上所有可能的IP地址,然后调用“arp -a”。

The following code works to find if a device is registered on an IP address, but I don't know how to get the MAC address from this. 以下代码用于查找设备是否在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);
        }

Any suggestions? 有什么建议么?

Try this 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();

    }

   }

}

This NetworkInterfaceNetworkInterface.getHardwareAddress() method is only allowed to access localhost MAC address, not remote host MAC address. NetworkInterfaceNetworkInterface.getHardwareAddress()方法仅允许访问localhost MAC地址,而不是远程主机MAC地址。

Note : You can't do this in remotely for PC or Device in java.But you can do it by using C# and WMI - WMI with netowrk in .NET language 注意:你无法在java中远程访问PC或设备。但是你可以使用C#和WMI - WMI与.NET语言中的netowrk一起使用

You're blindly assuming IPV4, which is not so reasonable anymore these days. 你盲目地假设IPV4,这些日子已经不再那么合理了。

And you're trying to dig out information that routers and access points have no good reason for disclosing in the first place (at least not to robots who won't authenticate themselves as an admin with access rights to the router's or access point's admin page). 而且你正在试图挖掘出路由器和接入点没有充分理由首先披露的信息(至少不是那些不会将自己认证为具有路由器或接入点管理页面访问权限的管理员的机器人) )。

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

相关问题 以编程方式将设备连接到android中的同一wifi网络? - Get Devices connected to same wifi network in android programmatically? 如何从 Android 应用程序检测 WiFi 网络中连接的所有设备 - How to detect all the Devices connected in a WiFi network from Android App 如何在Android中以编程方式在网络上查找其他WiFi设备的IP地址 - How to find IP address of OTHER WiFi Devices on network programmatically in android 如何在android中获取连接到同一网络的设备的mac地址? - How to get mac addresses of devices connected to the same network in android? 如何查找连接到套接字的Wi-Fi设备的MAC地址 - How to find the MAC address of Wi-Fi devices connected to socket 如何以编程方式通过Wifi获取连接到交换机网络的其他设备的IP地址? - How to get Ip address of other devices connected to switch network via Wifi programmatically? 如何使用NSD获取连接到同一网络的所有设备的列表 - How to get list of all devices connected to same network using NSD 如何获取WiFi网络接口的MAC地址? - How to obtain MAC address of WiFi network interface? 如何在android中准确扫描所有设备连接到wifi的IP和Mac地址? - How to scan IP and Mac address of all Device Connected to wifi in android accurately? 加快查找连接到Wifi网络的设备并获取设备名称的速度 - Speed up finding devices connected to a Wifi network and get device name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM