public class MacAddressFinder { /**
* Package access. Find the Mac address of the machine depending on the
* operating system.
* <p>
* @return null if we cannot find out.
*/
String getMacAddress() { try {
if(CommonUtils.isWindows()) {
return getWindowsMac();
}
else if(CommonUtils.isMacOSX()) {
return getOSXMac();
}
else if(CommonUtils.isSolaris()) {
return getSolarisMac();
}
else if(CommonUtils.isLinux()) {
getLinuxMac();
}
else {
return null;
}
} catch (IOException iox) {
return null;
}
return null;
} private String getWindowsMac() throws IOException {
String result = runCommand("ipconfig /all");
return parseResult(result,":");
} private String getOSXMac() throws IOException {
String result = runCommand("ifconfig -a");
return parseResult(result,"ether");
} private String getLinuxMac() throws IOException {
String result = runCommand("LANG=C /sbin/ifconfig");
if(result.length()<17)//unknown result, but it's gotta be bigger than 17
result = runCommand("LANG=C /bin/ifconfig");
if(result.length() < 17) //need to try another?
result = runCommand("LANG=C ifconfig");//getting desperate here.
return parseResult(result,"hwaddr");
} private String getSolarisMac() throws IOException {
String result = runCommand("ifconfig -a");//TODO1: correct command?
return parseResult(result,"ether");//TODO1: correct delimiter?
} private String parseResult(String result, String delimiter) {
result = result.toLowerCase();//lets ignore all case
StringTokenizer tok = new StringTokenizer(result,"\n");
while(tok.hasMoreTokens()) {//for each line of result
String line = tok.nextToken();
int index = line.indexOf(delimiter);
if(index >= 0) {//the line contains the delimiter
String address=line.substring(index+delimiter.length()).trim();
//address contains the rest of the line after the delimiter.
address = canonicalizeMacAddress(address);
if(address!=null)
return address;//null if in bad form
}
}
return null;
}
private String canonicalizeMacAddress(String address) {
if(address.length()!=17)
return null;
//check that we have six pair of numbers, separated by : or -
StringBuffer ret = new StringBuffer();
StringTokenizer tok = new StringTokenizer(address,":.-");
for(int i=0; i<6;i++) {
String val=null;
try {
val = tok.nextToken();
if(val.length()!=2)
return null;
} catch (NoSuchElementException nsex) {
return null;
}
ret.append(val);
if(i<5)
ret.append("-");
}
return ret.toString();
} /**
* @return the results of the command we just ran
* @param command the command - platform dependent.
*/
private String runCommand(String command) throws IOException {
//TODO1: make sure the path is set correctly, or we are not going to be
//able to execute the command
javax.swing.JOptionPane.showMessageDialog(null,"runCommand");
Process process=null;
try{
process = Runtime.getRuntime().exec(command);
}
catch(Exception err)
{
javax.swing.JOptionPane.showMessageDialog(null,err.getMessage()); } InputStream iStream = new BufferedInputStream(process.getInputStream());
StringBuffer buffer = new StringBuffer();//store the resutls
javax.swing.JOptionPane.showMessageDialog(null,"iStream");
while(true) {
int c = iStream.read();
if(c==-1) //eof?
break;
buffer.append((char)c);
}//buffer has all the data from the command.
iStream.close();
return buffer.toString();
}
// public static void main(String[] args) {
//MacAddressFinder f = new MacAddressFinder();
//System.out.println("The mac address is "+f.getMacAddress());
// }
}
* Package access. Find the Mac address of the machine depending on the
* operating system.
* <p>
* @return null if we cannot find out.
*/
String getMacAddress() { try {
if(CommonUtils.isWindows()) {
return getWindowsMac();
}
else if(CommonUtils.isMacOSX()) {
return getOSXMac();
}
else if(CommonUtils.isSolaris()) {
return getSolarisMac();
}
else if(CommonUtils.isLinux()) {
getLinuxMac();
}
else {
return null;
}
} catch (IOException iox) {
return null;
}
return null;
} private String getWindowsMac() throws IOException {
String result = runCommand("ipconfig /all");
return parseResult(result,":");
} private String getOSXMac() throws IOException {
String result = runCommand("ifconfig -a");
return parseResult(result,"ether");
} private String getLinuxMac() throws IOException {
String result = runCommand("LANG=C /sbin/ifconfig");
if(result.length()<17)//unknown result, but it's gotta be bigger than 17
result = runCommand("LANG=C /bin/ifconfig");
if(result.length() < 17) //need to try another?
result = runCommand("LANG=C ifconfig");//getting desperate here.
return parseResult(result,"hwaddr");
} private String getSolarisMac() throws IOException {
String result = runCommand("ifconfig -a");//TODO1: correct command?
return parseResult(result,"ether");//TODO1: correct delimiter?
} private String parseResult(String result, String delimiter) {
result = result.toLowerCase();//lets ignore all case
StringTokenizer tok = new StringTokenizer(result,"\n");
while(tok.hasMoreTokens()) {//for each line of result
String line = tok.nextToken();
int index = line.indexOf(delimiter);
if(index >= 0) {//the line contains the delimiter
String address=line.substring(index+delimiter.length()).trim();
//address contains the rest of the line after the delimiter.
address = canonicalizeMacAddress(address);
if(address!=null)
return address;//null if in bad form
}
}
return null;
}
private String canonicalizeMacAddress(String address) {
if(address.length()!=17)
return null;
//check that we have six pair of numbers, separated by : or -
StringBuffer ret = new StringBuffer();
StringTokenizer tok = new StringTokenizer(address,":.-");
for(int i=0; i<6;i++) {
String val=null;
try {
val = tok.nextToken();
if(val.length()!=2)
return null;
} catch (NoSuchElementException nsex) {
return null;
}
ret.append(val);
if(i<5)
ret.append("-");
}
return ret.toString();
} /**
* @return the results of the command we just ran
* @param command the command - platform dependent.
*/
private String runCommand(String command) throws IOException {
//TODO1: make sure the path is set correctly, or we are not going to be
//able to execute the command
javax.swing.JOptionPane.showMessageDialog(null,"runCommand");
Process process=null;
try{
process = Runtime.getRuntime().exec(command);
}
catch(Exception err)
{
javax.swing.JOptionPane.showMessageDialog(null,err.getMessage()); } InputStream iStream = new BufferedInputStream(process.getInputStream());
StringBuffer buffer = new StringBuffer();//store the resutls
javax.swing.JOptionPane.showMessageDialog(null,"iStream");
while(true) {
int c = iStream.read();
if(c==-1) //eof?
break;
buffer.append((char)c);
}//buffer has all the data from the command.
iStream.close();
return buffer.toString();
}
// public static void main(String[] args) {
//MacAddressFinder f = new MacAddressFinder();
//System.out.println("The mac address is "+f.getMacAddress());
// }
}
解决方案 »
- 我们大家一起来讨论一下Ioc容器接口的设计吧,共同进步
- javascript的一个小问题
- 关于iframe 中去浏览pdf
- Spring2.0,hibernate3.1,struts2整合问题
- IIS和TOMCAT集成后,上传文件失败,高手们不惜赐教!!
- java相关问题
- 在jsp/servlet中怎样循环发送请求?
- tomcat 5.0.29服务器,spring 中 配置 hibernate 的数据库 连接池(用tomcat的连接池)问题?
- SOA ?怎么实现这个框架好呢?
- 请filippo1980(管药师★吾辈岂是池中物,一遇风云便化龙)和keke1(龙)进来领分!谢谢你们的帮助,请再谈谈你们对XML的前景的看法!
- ■■困惑,使用JBuilder按理CMP EntityBean。■■
- 这段关于反射的代码为啥出错? 谢谢!
if(CommonUtils.isWindows()) {
return getWindowsMac();中的CommonUtils是什么,找不到呀
import java.util.*;
//import java.awt.*;
import java.net.*;
import java.io.*;
import java.lang.*;
//import java.math.*;
//import java.utils.*;public class MacAddressFinder { /**
* Package access. Find the Mac address of the machine depending on the
* operating system.
* <p>
* @return null if we cannot find out.
*/
String getMacAddress() { try {
//if(CommonUtils.isWindows()) {
return getWindowsMac();
/* }
else if(CommonUtils.isMacOSX()) {
return getOSXMac();
}
else if(CommonUtils.isSolaris()) {
return getSolarisMac();
}
else if(CommonUtils.isLinux()) {
getLinuxMac();
}
else {
return null;
}
*/
} catch (IOException iox) {
return null;
} //return null;
} private String getWindowsMac() throws IOException {
String result = runCommand("ipconfig /all");
return parseResult(result,":");
} private String getOSXMac() throws IOException {
String result = runCommand("ifconfig -a");
return parseResult(result,"ether");
} private String getLinuxMac() throws IOException {
String result = runCommand("LANG=C /sbin/ifconfig");
if(result.length()<17)//unknown result, but it's gotta be bigger than 17
result = runCommand("LANG=C /bin/ifconfig");
if(result.length() < 17) //need to try another?
result = runCommand("LANG=C ifconfig");//getting desperate here.
return parseResult(result,"hwaddr");
} private String getSolarisMac() throws IOException {
String result = runCommand("ifconfig -a");//TODO1: correct command?
return parseResult(result,"ether");//TODO1: correct delimiter?
} private String parseResult(String result, String delimiter) {
result = result.toLowerCase();//lets ignore all case
StringTokenizer tok = new StringTokenizer(result,"\n");
while(tok.hasMoreTokens()) {//for each line of result
String line = tok.nextToken();
int index = line.indexOf(delimiter);
if(index >= 0) {//the line contains the delimiter
String address=line.substring(index+delimiter.length()).trim();
//address contains the rest of the line after the delimiter.
address = canonicalizeMacAddress(address);
if(address!=null)
return address;//null if in bad form
}
}
return null;
}
private String canonicalizeMacAddress(String address) {
if(address.length()!=17)
return null;
//check that we have six pair of numbers, separated by : or -
StringBuffer ret = new StringBuffer();
StringTokenizer tok = new StringTokenizer(address,":.-");
for(int i=0; i<6;i++) {
String val=null;
try {
val = tok.nextToken();
if(val.length()!=2)
return null;
} catch (NoSuchElementException nsex) {
return null;
}
ret.append(val);
if(i<5)
ret.append("-");
}
return ret.toString();
} /**
* @return the results of the command we just ran
* @param command the command - platform dependent.
*/
private String runCommand(String command) throws IOException {
//TODO1: make sure the path is set correctly, or we are not going to be
//able to execute the command
javax.swing.JOptionPane.showMessageDialog(null,"runCommand");
Process process=null;
try{
process = Runtime.getRuntime().exec(command);
}
catch(Exception err)
{
javax.swing.JOptionPane.showMessageDialog(null,err.getMessage()); } InputStream iStream = new BufferedInputStream(process.getInputStream());
StringBuffer buffer = new StringBuffer();//store the resutls
javax.swing.JOptionPane.showMessageDialog(null,"iStream");
while(true) {
int c = iStream.read();
if(c==-1) //eof?
break;
buffer.append((char)c);
}//buffer has all the data from the command.
iStream.close();
return buffer.toString();
}
public static void main(String[] args) {
MacAddressFinder f = new MacAddressFinder();
System.out.println("The mac address is "+f.getMacAddress());
}
}
接分!
你现在取的是Physical Address而不是MAC
二是使用了java对字符串的过滤和分析。