当前位置:   article > 正文

Java获取IP地址以及MAC地址(附Demo)_java 获取电脑唯一真是mac地址

java 获取电脑唯一真是mac地址

前言

需要获取客户端的IP地址以及MAC地址

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test {

    public static void main(String[] args) {
        try {
            // 执行命令
            Process process = Runtime.getRuntime().exec("ipconfig /all");
            // 读取命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                // 输出命令结果
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

纯用CMD可能权限不够,可能格式可能乱码等问题

在这里插入图片描述

后续转为网络编程API接口

网络适配器的 IPv4 和 MAC 地址,最好直接使用 Java 的网络编程 API,而不是通过执行系统命令来获取,可以使用 java.net.NetworkInterface 类来获取网络接口的信息,然后进一步筛选出所需的适配器信息

  • 在获取本地主机信息时,要考虑多网卡的情况,确保准确获取所需的网络适配器信息
  • 对于操作系统信息的获取,可以考虑使用更可靠的方式,如 System.getProperty() 方法

1. IP及MAC

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class test {

    public static void main(String[] args) {
        try {
            // 获取所有网络接口
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                // 输出网络接口名称
                System.out.println("Network Interface: " + networkInterface.getDisplayName());
                // 获取该网络接口的所有地址
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    // 输出地址信息
                    System.out.println("  Address: " + address.getHostAddress());
                }
                // 获取 MAC 地址
                byte[] mac = networkInterface.getHardwareAddress();
                if (mac != null) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                    // 输出 MAC 地址
                    System.out.println("  MAC Address: " + sb.toString());
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

截图如下:

在这里插入图片描述

2. 特定适配器

不同电脑可能特定的适配器不大一样,具体Demo看自身

package com.example.test;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class test {

    public static void main(String[] args) {
        try {
            // 获取所有网络接口
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface networkInterface = interfaces.nextElement();
                // 检查是否是你想要的网络适配器,这里假设名字为 "VMware Network Adapter VMnet8"
                if (networkInterface.getDisplayName().equals("VMware Virtual Ethernet Adapter for VMnet8")) {
                    // 获取该网络接口的所有地址
                    Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
                        InetAddress address = addresses.nextElement();
                        // 过滤 IPv4 地址
                        if (address instanceof java.net.Inet4Address) {
                            // 输出 IPv4 地址
                            System.out.println("IPv4 Address: " + address.getHostAddress());
                        }
                    }
                    // 获取 MAC 地址
                    byte[] mac = networkInterface.getHardwareAddress();
                    if (mac != null) {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < mac.length; i++) {
                            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                        }
                        // 输出 MAC 地址
                        System.out.println("MAC Address: " + sb.toString());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

截图如下:

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/778524
推荐阅读
相关标签
  

闽ICP备14008679号