网上搜到一段取mac的代码,刚好用到,存下,改进使用正则表达式来匹配mac
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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;
}
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;
}