通过mac地址获取局域网IP

这是一个Java程序,用于获取本机网内地址,并通过MAC地址查找局域网内的IP。代码首先遍历所有网络接口,过滤掉回环、虚拟、未启用或含有VM字样的接口,然后获取Inet4Address。接着,通过执行'arp-a'命令,解析输出,匹配MAC地址和IP,建立映射关系。最后,根据输入的MAC地址返回对应的IP地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.zsy.gulimall.product.test;

import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通过mac地址获取局域网IP
 * 只在开发环境下生效
 * @author YuLF
 * @since 2021-01-13 10:37
 */
@Slf4j
public class LanIpResolver {


    public LanIpResolver() {
    }

    private static final Pattern IP_PATTERN = Pattern.compile("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
    private static final Pattern MAC_PATTERN = Pattern.compile("([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}");

    /**
     * 获取本机网内地址
     */
    public static InetAddress getNet4Address() {
        try {
            //获取所有网络接口
            Enumeration<NetworkInterface> allNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
            //遍历所有网络接口
            while (allNetworkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = allNetworkInterfaces.nextElement();
                //如果此网络接口为 回环接口 或者 虚拟接口(子接口) 或者 未启用 或者 描述中包含VM
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp() || networkInterface.getDisplayName().contains("VM")) {
                    continue;
                }
                for (Enumeration<InetAddress> netAddressEnumeration = networkInterface.getInetAddresses(); netAddressEnumeration.hasMoreElements(); ) {
                    InetAddress inetAddress = netAddressEnumeration.nextElement();
                    if (inetAddress != null) {
                        if (inetAddress instanceof Inet4Address) {
                            return inetAddress;
                        }
                    }
                }
            }
            return null;
        } catch (SocketException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过mac地址获取局域网ip
     * @author YuLF
     * @since  2021/1/15 16:24
     */
    public static String getLanIpByMac(String macAddr){
        InetAddress net4Address = getNet4Address();
        assert net4Address != null;
        String hostAddress = net4Address.getHostAddress();
        log.info("局域网IP:{}", hostAddress);
        Map<String, String> ipMap = new HashMap<>(32);
        try {
            Process process = Runtime.getRuntime().exec("arp -a");
            @Cleanup InputStream is = process.getInputStream();
            @Cleanup InputStreamReader isr = new InputStreamReader(is);
            @Cleanup BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                String mac = getMac(line);
                if(mac != null){
                    ipMap.put(mac, getIp(line));
                }
            }
        } catch (IOException e) {
            log.info("com.jsy.community.utils.LanIpResolver.getLanIpByMac:获取局域网IP失败:{}", e.getMessage());
            return null;
        }
        return ipMap.get(macAddr);
    }

    public static void main(String[] args) {
        System.out.println("陈春利的IP:"+getLanIpByMac("3c-7c-3f-4b-c0-a0"));
    }

    private static String getIp(String ipStr) {
        Matcher matcher = IP_PATTERN.matcher(ipStr);
        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }

    private static String getMac(String ipStr){
        Matcher matcher = MAC_PATTERN.matcher(ipStr);
        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员小小刘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值