java 怎样获取本地连接的mac地址,我有多张网卡,且是用无线网卡上网
展开全部
public static String getMAC(String ipAddress) {
String address = "ERROR";
// if(ipAddress.equalsIgnoreCase("localhost")||ipAddress.equalsIgnoreCase("127.0.0.1")){
// return address ;
// }
String os = System.getProperty("os.name");
if (os != null && os.startsWith("Windows")) {
try {
String command = "cmd.exe /c nbtstat -a "+ipAddress;
Process p = Runtime.getRuntime().exec(command);
BufferedReader br =
new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("MAC") > 0) {
int index = line.indexOf("=");
index+=2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {}
}
return address;
}
String address = "ERROR";
// if(ipAddress.equalsIgnoreCase("localhost")||ipAddress.equalsIgnoreCase("127.0.0.1")){
// return address ;
// }
String os = System.getProperty("os.name");
if (os != null && os.startsWith("Windows")) {
try {
String command = "cmd.exe /c nbtstat -a "+ipAddress;
Process p = Runtime.getRuntime().exec(command);
BufferedReader br =
new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("MAC") > 0) {
int index = line.indexOf("=");
index+=2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {}
}
return address;
}
更多追问追答
追问
那像我目前的情况,ipAddress传入什么
追答
我不知道啊 你的外网地址是什么
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询