1. 使用Java原生API获取WiFi IP地址和MAC地址

获取WiFi IP地址:

try {
    InetAddress ip = InetAddress.getLocalHost();
    System.out.println('IP address: ' + ip.getHostAddress());
} catch (UnknownHostException e) {
    e.printStackTrace();
}

获取WiFi MAC地址:

try {
    NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
    byte[] mac = network.getHardwareAddress();
    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('MAC address: ' + sb.toString());
} catch (SocketException | UnknownHostException e) {
    e.printStackTrace();
}
  1. 使用Apache Commons Net库获取WiFi IP地址和MAC地址

下载地址:https://commons.apache.org/proper/commons-net/

获取WiFi IP地址:

try (DatagramSocket socket = new DatagramSocket()) {
    socket.connect(InetAddress.getByName('8.8.8.8'), 10002);
    String ip = socket.getLocalAddress().getHostAddress();
    System.out.println('IP address: ' + ip);
} catch (SocketException | UnknownHostException e) {
    e.printStackTrace();
}

获取WiFi MAC地址:

try {
    InetAddress ip = InetAddress.getLocalHost();
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    byte[] mac = network.getHardwareAddress();
    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('MAC address: ' + sb.toString());
} catch (SocketException | UnknownHostException e) {
    e.printStackTrace();
}
  1. 使用JavaFX库获取WiFi IP地址和MAC地址

获取WiFi IP地址:

String ip = Inet4Address.getLocalHost().getHostAddress();
System.out.println('IP address: ' + ip);

获取WiFi MAC地址:

String mac = '';
try {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface ni = networkInterfaces.nextElement();
        byte[] hardwareAddress = ni.getHardwareAddress();
        if (hardwareAddress != null) {
            StringBuilder sb = new StringBuilder();
            for (byte b : hardwareAddress) {
                sb.append(String.format('%02X ', b));
            }
            mac = sb.toString().trim();
            break;
        }
    }
} catch (SocketException e) {
    e.printStackTrace();
}
System.out.println('MAC address: ' + mac);
  1. 使用Jpcap库获取WiFi IP地址和MAC地址

下载地址:https://github.com/rainerfrey/jpcap

获取WiFi IP地址:

try {
    NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    for (NetworkInterface device : devices) {
        if (device.name.equals('wlan0')) {
            System.out.println('IP address: ' + device.addresses[0].address);
            break;
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

获取WiFi MAC地址:

try {
    NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    for (NetworkInterface device : devices) {
        if (device.name.equals('wlan0')) {
            byte[] mac = device.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('MAC address: ' + sb.toString());
            break;
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}
  1. 使用jNetPcap库获取WiFi IP地址和MAC地址

下载地址:https://github.com/ruedigergad/jnetpcap

获取WiFi IP地址:

try {
    List<PcapIf> allDevs = new ArrayList<>();
    StringBuilder errbuf = new StringBuilder();
    int r = Pcap.findAllDevs(allDevs, errbuf);
    if (r == Pcap.NOT_OK || allDevs.isEmpty()) {
        System.err.println('Can't read list of devices, error is ' + errbuf.toString());
        return;
    }
    for (PcapIf pcapIf : allDevs) {
        if (pcapIf.getDescription().contains('wlan0')) {
            List<PcapAddr> addresses = pcapIf.getAddresses();
            for (PcapAddr address : addresses) {
                InetAddress ip = InetAddress.getByAddress(address.getAddr().getData());
                if (ip instanceof Inet4Address) {
                    System.out.println('IP address: ' + ip.getHostAddress());
                    break;
                }
            }
            break;
        }
    }
} catch (UnknownHostException e) {
    e.printStackTrace();
}

获取WiFi MAC地址:

try {
    List<PcapIf> allDevs = new ArrayList<>();
    StringBuilder errbuf = new StringBuilder();
    int r = Pcap.findAllDevs(allDevs, errbuf);
    if (r == Pcap.NOT_OK || allDevs.isEmpty()) {
        System.err.println('Can't read list of devices, error is ' + errbuf.toString());
        return;
    }
    for (PcapIf pcapIf : allDevs) {
        if (pcapIf.getDescription().contains('wlan0')) {
            byte[] mac = pcapIf.getHardwareAddress();
            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('MAC address: ' + sb.toString());
            break;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
5种Java获取WiFi IP地址和MAC地址的方法及示例

原文地址: https://www.cveoy.top/t/topic/mPk0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录