java取mac地址

网上搜到一段取mac的代码,刚好用到,存下,改进使用正则表达式来匹配mac

获取mac地址
public String getMACAddress(String ip) {
String str
= "";
String macAddress
= "";
try {
Process p
= Runtime.getRuntime().exec("nbtstat -A " + ip);
InputStream inpput
= p.getInputStream();
String content
= IOUtils.toString(inpput);

Pattern pattern
= Pattern.compile("\\w{2}-\\w{2}-\\w{2}-\\w{2}-\\w{2}-\\w{2}");
Matcher matcher
= pattern.matcher(content);

if (matcher.find()) {
macAddress
= matcher.group();
}
}
catch (IOException e) {
e.printStackTrace(System.out);
}
return macAddress;
}
原文地址:https://www.cnblogs.com/xiziyin/p/1664845.html