java

位置:IT落伍者 >> java >> 浏览文章

Java实现读取本机网卡Mac地址


发布日期:2022年02月18日
 
Java实现读取本机网卡Mac地址

本方法主要用来限制系统在其他的机器上运行其实原理简单的很没有调用第三方插件代码如下:

package comusersutil;

import javaio*;

import javautil*;

import javautilregex*;

public class NetID {

String IPCONFIG_COMMAND_WIN = ipconfig /all;

boolean realMac = true;

String unique = ;

public static String getMacAddress() {

NetID hwid = new NetID();

return hwidgetUnique()trim();

}

private String getUnique() {

String os = SystemgetProperty(osname);

if (osstartsWith(Windows)) {

return getUniqueWindows();

}else {

return ;

}

}

private String getUniqueWindows() {

String ipConfigResponse = null;

try {

ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND_WIN);

}

catch (IOException e) {

eprintStackTrace();

}

StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse \n);

while (tokenizerhasMoreTokens()) {

String line = tokenizernextToken()trim();

int macAddressPosition = lineindexOf(:);

if (macAddressPosition <= ) {

continue;

}

String macAddressCandidate = linesubstring(macAddressPosition + )

trim();

if (isMacAddWin(macAddressCandidate)) {

if (realMac == true) {

generateUnique(macAddressCandidate);

}

else {

realMac = true;

}

}

}

return unique;

}

private String runConsoleCommand(String command) throws IOException {

Process p = RuntimegetRuntime()exec(command);

InputStream stdoutStream = new BufferedInputStream(pgetInputStream());

StringBuffer buffer = new StringBuffer();

while (true) {

int c = stdoutStreamread();

if (c == ) {

break;

}

bufferappend( (char) c);

}

String outputText = buffertoString();

stdoutStreamclose();

return outputText;

}

private boolean isMacAddWin(String macAddressCandidate) {

Pattern macPattern = pile([afAF]{}[afAF]{}[afAF]{}[afAF]{}[afAF]{}[afAF]{});

Matcher m = macPatternmatcher(macAddressCandidate);

return mmatches();

}

private boolean isMacAddOSX(String macAddressCandidate) {

if (macAddressCandidatelength() != ) {

return false;

}

else {

return true;

}

}

private void generateUnique(String macAddress) {

if (unique == ) {

unique += macAddress;

}

else {

unique += #;

unique += macAddress;

}

}

public static void main(String [] args) {

Systemoutprintln(NetIDgetMacAddress());

}

}

               

上一篇:Java类 精彩问答

下一篇:浅谈Java对象的序列化和反序列化