小编典典
我写了自己的方法来解决我的问题。在这里,是否有人需要代码来查找同一网络中的MAC地址。在Win 7和Mac OS X
10.8.2上无需任何管理员权限即可为我工作
Pattern macpt = null;
private String getMac(String ip) {
// Find OS and set command according to OS
String OS = System.getProperty("os.name").toLowerCase();
String[] cmd;
if (OS.contains("win")) {
// Windows
macpt = Pattern
.compile("[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+");
String[] a = { "arp", "-a", ip };
cmd = a;
} else {
// Mac OS X, Linux
macpt = Pattern
.compile("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+");
String[] a = { "arp", ip };
cmd = a;
}
try {
// Run command
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
// read output with BufferedReader
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = reader.readLine();
// Loop trough lines
while (line != null) {
Matcher m = macpt.matcher(line);
// when Matcher finds a Line then return it as result
if (m.find()) {
System.out.println("Found");
System.out.println("MAC: " + m.group(0));
return m.group(0);
}
line = reader.readLine();
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Return empty string if no MAC is found
return "";
}
2020-11-13