java获取机器ip、mac

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Enumeration;

import java.util.List;

public class NetUtil {

public static String getIps(){

List<String> ips= new ArrayList<>();

try {

Enumeration<NetworkInterface> allNetInterfaces;

allNetInterfaces = NetworkInterface.getNetworkInterfaces();

while (allNetInterfaces.hasMoreElements()) {

NetworkInterface netInterface = allNetInterfaces.nextElement();

if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint()) {

//just print

Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();

InetAddress iaddr;

while (inetAddresses.hasMoreElements()) {

iaddr = inetAddresses.nextElement();

if (iaddr != null) {

//ips.add(iaddr.getHostAddress());

System.out.println("aa:" + iaddr.getHostAddress());

}

}

continue;

}

Enumeration<InetAddress> inetAddresses = netInterface.getInetAddresses();

InetAddress iaddr;

while (inetAddresses.hasMoreElements()) {

iaddr = inetAddresses.nextElement();

if (iaddr != null) {

ips.add(iaddr.getHostAddress());

}

}

}

} catch (Exception ignored) {

}

Collections.sort(ips);

StringBuilder sb=new StringBuilder();

for(String mac:ips){

sb.append(mac);

}

return sb.toString();

}

public static String getMac(){

List<String> macs= new ArrayList<>();

try {

Enumeration<NetworkInterface> allNetInterfaces;

allNetInterfaces = NetworkInterface.getNetworkInterfaces();

byte[] mac;

while (allNetInterfaces.hasMoreElements()) {

NetworkInterface netInterface = allNetInterfaces.nextElement();

if (netInterface.isLoopback() || netInterface.isVirtual() || netInterface.isPointToPoint()) {

mac = netInterface.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) ? "-" : ""));

}

if (sb.length() > 0) {

//macs.add(sb.toString());

System.out.println("aa:" + sb.toString());

}

}

continue;

}

mac = netInterface.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) ? "-" : ""));

}

if (sb.length() > 0) {

macs.add(sb.toString());

}

}

}

} catch (Exception ignored) {

}

Collections.sort(macs);

StringBuilder sb=new StringBuilder();

for(String mac:macs){

sb.append(mac);

}

return sb.toString();

}

public static void main(String[] args) {

String ret = getIps();

ret = getMac();

System.out.println(ret);

}

}

相关推荐
碎梦归途30 分钟前
23种设计模式-行为型模式之状态模式(Java版本)
java·jvm·设计模式·状态模式·软考·软件设计师·行为模式
豆沙沙包?2 小时前
2025年- H16-Lc124-169.多数元素(技巧)---java版
java·开发语言
zhangxueyi2 小时前
Java实现基数排序算法
java·数据结构·算法
Another Iso4 小时前
同时启动俩个tomcat压缩版
java·tomcat
华纳云IDC服务商5 小时前
华纳云:centos如何实现JSP页面的动态加载
java·linux·centos
碎梦归途5 小时前
23种设计模式-行为型模式之访问者模式(Java版本)
java·开发语言·jvm·设计模式·软考·软件设计师·行为型模式
Demons_kirit5 小时前
LeetCode LCP40 心算挑战题解
java·数据结构·算法·leetcode·职场和发展
Jiaberrr6 小时前
uniapp 实现低功耗蓝牙连接并读写数据实战指南
java·前端·javascript·vue.js·struts·uni-app
家乡的落日6 小时前
一、I/O的相关概念
java
码熔burning6 小时前
【MQ篇】RabbitMQ之死信交换机!
java·分布式·rabbitmq·mq