使用java获取自己的机器网卡

package org.ibase4j.core.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public final class NativeUtil {
private static Logger logger = LogManager.getLogger();

private NativeUtil() {
}

/** 获取机器名 */
public static final String getHostName() {
String hostName = "";
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
logger.error(e.getMessage(), e);
}
return hostName;
}

/** 获取网卡序列号 */
public static final String getDUID() {
String address = "";
String command = "cmd.exe /c ipconfig /all";
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("DUID") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
} catch (IOException e) {
}
return address;
}
}

posted on 2017-03-31 21:32  zzz初见  阅读(2488)  评论(0编辑  收藏  举报

导航