java 获得MAC Address
目前,正在做广告联盟的程序,涉及到作弊这一块,用cookie毕竟可以清除,于是就想到了mac,到网上搜索半天,没有找到相关的文章,于是尝试找英文的,谁知道一找便发现一篇关于获得MAC Address,觉得不错,于是帖出供大家分享.
来源:http://forums.java.sun.com/thread.jspa?threadID=245711&tstart=240


/*
* 创建日期 2005-7-21
* @author jason
* @email sunchj2001@163.com
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/

import java.net.InetAddress;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.StringTokenizer;

public final class NetworkInfo {
private final static String getMacAddress() throws IOException {
String os = System.getProperty(
"os.name");

try {
if (os.startsWith(
"Windows")) {
return windowsParseMacAddress(windowsRunIpConfigCommand());
} else if (os.startsWith(
"Linux")) {
return linuxParseMacAddress(linuxRunIfConfigCommand());
} else {
throw new IOException(
"unknown operating system: " + os);
}
} catch (ParseException ex) {
ex.printStackTrace();
throw new IOException(ex.getMessage());
}
}

/*
* Linux stuff
*/

private final static String linuxParseMacAddress(String ipConfigResponse)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}

StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse,
"\n");
String lastMacAddress = null;

while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
boolean containsLocalHost = line.indexOf(localHost) >= 0;

// see if line contains IP address
if (containsLocalHost && lastMacAddress != null) {
return lastMacAddress;
}

// see if line contains MAC address
int macAddressPosition = line.indexOf(
"HWaddr");
if (macAddressPosition <= 0)
continue;

String macAddressCandidate = line.substring(macAddressPosition + 6)
.trim();
if (linuxIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}

ParseException ex = new ParseException(
"cannot read MAC address for "
+ localHost +
" from [" + ipConfigResponse + "]", 0);
ex.printStackTrace();
throw ex;
}

private final static boolean linuxIsMacAddress(String macAddressCandidate) {
// TODO: use a smart regular expression
if (macAddressCandidate.length() != 17)
return false;
return true;
}

private final static String linuxRunIfConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec(
"ifconfig");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());

StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();

stdoutStream.close();

return outputText;
}

/*
* Windows stuff
*/

private final static String windowsParseMacAddress(String ipConfigResponse)
throws ParseException {
String localHost = null;
try {
localHost = InetAddress.getLocalHost().getHostAddress();
} catch (java.net.UnknownHostException ex) {
ex.printStackTrace();
throw new ParseException(ex.getMessage(), 0);
}

StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse,
"\n");
String lastMacAddress = null;

while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();

// see if line contains IP address
if (line.endsWith(localHost) && lastMacAddress != null) {
return lastMacAddress;
}

// see if line contains MAC address
int macAddressPosition = line.indexOf(
":");
if (macAddressPosition <= 0)
continue;

String macAddressCandidate = line.substring(macAddressPosition + 1)
.trim();
if (windowsIsMacAddress(macAddressCandidate)) {
lastMacAddress = macAddressCandidate;
continue;
}
}

ParseException ex = new ParseException(
"cannot read MAC address from ["
+ ipConfigResponse +
"]", 0);
ex.printStackTrace();
throw ex;
}

private final static boolean windowsIsMacAddress(String macAddressCandidate) {
// TODO: use a smart regular expression
if (macAddressCandidate.length() != 17)
return false;

return true;
}

private final static String windowsRunIpConfigCommand() throws IOException {
Process p = Runtime.getRuntime().exec(
"ipconfig /all");
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());

StringBuffer buffer = new StringBuffer();
for (;;) {
int c = stdoutStream.read();
if (c == -1)
break;
buffer.append((char) c);
}
String outputText = buffer.toString();

stdoutStream.close();

return outputText;
}

/*
* Main
*/

public final static void main(String[] args) {
try {
System.out.println(
"Network infos");

System.out.println(
" Operating System: "
+ System.getProperty(
"os.name"));
System.out.println(
" IP/Localhost: "
+ InetAddress.getLocalHost().getHostAddress());
System.out.println(
" MAC Address: " + getMacAddress());
} catch (Throwable t) {
t.printStackTrace();
}
}
}

Jason   2005-07-21 09:28:43 评论:6   阅读:2553   引用:0
无题 @2005-07-21 19:11:15  Jason
这个获得mac地址的程序只是获得服务器端的,可惜我真正要的是客户端的Mac 地址.
中文问题测试 @2005-07-21 17:14:41  hofman
中午修改系统导致的中文问题现在应该解决了。

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

小圣空间 版权没有 盗版必就