参考:http://www.jb51.net/article/73882.htm
/** * 获取MAC地址 * @author * 2011-12 */ public static class GetMacAddress { public String callCmd(String[] cmd) { String result = ""; String line = ""; try { Process proc = Runtime.getRuntime().exec(cmd); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); while ((line = br.readLine ()) != null) { result += line; } } catch(Exception e) { e.printStackTrace(); } return result; } /** * * @param cmd 第一个命令 * @param another 第二个命令 * @return 第二个命令的执行结果 */ public String callCmd(String[] cmd,String[] another) { String result = ""; String line = ""; try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd); proc.waitFor(); //已经执行完第一个命令,准备执行第二个命令 proc = rt.exec(another); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); while ((line = br.readLine ()) != null) { result += line; } } catch(Exception e) { e.printStackTrace(); } return result; } /** * * @param ip 目标ip,一般在局域网内 * @param sourceString 命令处理的结果字符串 * @param macSeparator mac分隔符号 * @return mac地址,用上面的分隔符号表示 */ public String filterMacAddress(final String ip, String sourceString,final String macSeparator) { String result = ""; int index=sourceString.indexOf(ip); if(index==-1){ index=0; } sourceString=sourceString.substring(index,sourceString.length()-1); String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})"; Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(sourceString); while(matcher.find()){ result = matcher.group(1); if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) { break; //如果有多个IP,只匹配本IP对应的Mac. } } return result; } /** * * @param ip 目标ip * @return Mac Address * */ public String getMacInWindows(final String ip){ String result = ""; String[] cmd = { "cmd", "/c", "ping " + ip }; String[] another = { "cmd", "/c", "arp -a" }; String cmdResult = callCmd(cmd,another); result = filterMacAddress(ip,cmdResult,"-"); return result; } /** * @param ip 目标ip * @return Mac Address * */ public String getMacInLinux(final String ip){ String result = ""; String[] cmd = { "/bin/sh", "-c", "ping " + ip + " -c 2 && arp -a" }; String cmdResult = callCmd(cmd); result = filterMacAddress(ip,cmdResult,":"); return result; } /** * 获取MAC地址 * @return 返回MAC地址 */ public String getMacAddress(String ip){ String macAddress = ""; macAddress = getMacInWindows(ip).trim(); if(macAddress==null||"".equals(macAddress)){ macAddress = getMacInLinux(ip).trim(); } return macAddress; } } // //做个测试 public static void main(String[] args) { System.out.println(new GetMacAddress().getMacAddress("192.168.0.105")); System.out.println(new GetMacAddress().getMacAddress("220.181.111.148")); }
这个是局域网的:
package
com.example;
import
java.io.InputStream;
import
java.util.regex.Matcher;
import
java.util.regex.Pattern;
public
class
Cmd {
/**
* 执行单条指令
* @param cmd 命令
* @return 执行结果
* @throws Exception
*/
public
static
String command(String cmd)
throws
Exception{
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
InputStream in = process.getInputStream();
StringBuilder result =
new
StringBuilder();
byte
[] data =
new
byte
[
256
];
while
(in.read(data) != -
1
){
String encoding = System.getProperty(
"sun.jnu.encoding"
);
result.append(
new
String(data,encoding));
}
return
result.toString();
}
/**
* 获取mac地址
* @param ip
* @return
* @throws Exception
*/
public
static
String getMacAddress(String ip)
throws
Exception{
String result = command(
"ping "
+ip+
" -n 2"
);
if
(result.contains(
"TTL"
)){
result = command(
"arp -a "
+ip);
}
String regExp =
"([0-9A-Fa-f]{2})([-:][0-9A-Fa-f]{2}){5}"
;
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(result);
StringBuilder mac =
new
StringBuilder();
while
(matcher.find()) {
String temp = matcher.group();
mac.append(temp);
}
return
mac.toString();
}
public
static
void
main(String[] args)
throws
Exception {
System.out.println(System.currentTimeMillis());
System.out.println(getMacAddress(
"192.168.1.113"
));
System.out.println(System.currentTimeMillis());
//System.out.println(command("arp -a 192.168.1.103"));
//System.out.println(System.getProperties());
}
}
这个没成功:
// 获取非本地IP的MAC地址 try { String ipAddress="220.181.111.148"; System.out.println(ipAddress); Process p = Runtime.getRuntime().exec("nbtstat -A " + ipAddress); System.out.println("===process=="+p); InputStreamReader ir = new InputStreamReader(p.getInputStream()); BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream(),"gb2312")); // BufferedReader br = new BufferedReader(ir); String str; while ((str = br.readLine()) != null) { if(str.indexOf("MAC")>1){ String macAddress = str.substring(str.indexOf("MAC")+9, str.length()); macAddress = macAddress.trim(); System.out.println("macAddress:" + macAddress); break; } } p.destroy(); br.close(); ir.close(); } catch (IOException e) { e.printStackTrace(); }
获取本机mac地址:
String str = "";
String macAddress = "";
final String LOOPBACK_ADDRESS = "127.0.0.1";
// 如果为127.0.0.1,则获取本地MAC地址。
if (LOOPBACK_ADDRESS.equals(ipAddress)) {
InetAddress inetAddress = InetAddress.getLocalHost();
// 貌似此方法需要JDK1.6。
byte[] mac = NetworkInterface.getByInetAddress(inetAddress)
.getHardwareAddress();
// 下面代码是把mac地址拼装成String
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
if (i != 0) {
sb.append("-");
}
// mac[i] & 0xFF 是为了把byte转化为正整数
String s = Integer.toHexString(mac[i] & 0xFF);
sb.append(s.length() == 1 ? 0 + s : s);
}
// 把字符串所有小写字母改为大写成为正规的mac地址并返回
macAddress = sb.toString().trim().toUpperCase();
return macAddress;
}