Java获取IP地址以及MAC地址(附Demo)

前言

需要获取客户端的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();
        }
    }
}

纯用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();
        }
    }
}

截图如下:

在这里插入图片描述

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();
        }
    }
}

截图如下:

在这里插入图片描述

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值