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

}

}

相关推荐
小张认为的测试18 分钟前
Liunx上Jenkins 持续集成 Java + Maven + TestNG + Allure + Rest-Assured 接口自动化项目
java·ci/cd·jenkins·maven·接口·testng
蘑菇丁1 小时前
ansible批量生产kerberos票据,并批量分发到所有其他主机脚本
java·ide·eclipse
呼啦啦啦啦啦啦啦啦2 小时前
【Redis】持久化机制
java·redis·mybatis
我想学LINUX3 小时前
【2024年华为OD机试】 (A卷,100分)- 微服务的集成测试(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·微服务·集成测试
空の鱼7 小时前
java开发,IDEA转战VSCODE配置(mac)
java·vscode
P7进阶路8 小时前
Tomcat异常日志中文乱码怎么解决
java·tomcat·firefox
小丁爱养花9 小时前
Spring MVC:HTTP 请求的参数传递2.0
java·后端·spring
CodeClimb9 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
等一场春雨9 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)
java·设计模式·桥接模式