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

}

}

相关推荐
飞升不如收破烂~19 分钟前
Spring boot常用注解和作用
java·spring boot·后端
秦老师Q19 分钟前
Java基础第九章-Java集合框架(超详细)!!!
java·开发语言
计算机毕设源码qq-383653104121 分钟前
(附项目源码)Java开发语言,215 springboot 大学生爱心互助代购网站,计算机毕设程序开发+文案(LW+PPT)
java·开发语言·spring boot·mysql·课程设计
ashane131423 分钟前
Java list
java·windows·list
袁庭新30 分钟前
Cannal实现MySQL主从同步环境搭建
java·数据库·mysql·计算机·java程序员·袁庭新
无尽的大道32 分钟前
深入理解 Java 阻塞队列:使用场景、原理与性能优化
java·开发语言·性能优化
岁岁岁平安1 小时前
springboot实战(15)(注解@JsonFormat(pattern=“?“)、@JsonIgnore)
java·spring boot·后端·idea
Oak Zhang1 小时前
TheadLocal出现的内存泄漏具体泄漏的是什么?弱引用在里面有什么作用?什么情景什么问题?
java·系统安全
数据小小爬虫1 小时前
如何利用Java爬虫获得1688店铺详情
java·开发语言
天若有情6731 小时前
c++框架设计展示---提高开发效率!
java·c++·算法