简体   繁体   English

如何获取java中local.network上机器的MAC地址

[英]How to get the MAC address of machine on local network in java

i am using NetworkInterface class to get the MAC address but i am getting null in my code NetworkInterface ni = NetworkInterface.getBy.netAddress(.netAddr);我正在使用 NetworkInterface class 获取 MAC 地址,但我在代码 NetworkInterface ni = NetworkInterface.getBy.netAddress(.netAddr) 中得到 null; .I am getting null in ni object,pleas suggest me the way to get the mac address of a device on lan. .I get null in ni object,pleas suggest me the way to get the mac address of a device on lan.

Thanks in advance.提前致谢。

Try this code,试试这段代码,

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;

public class GetMac
{
public static void main(String[] args)
throws IOException
{
String address = new GetMac().getMacAddress();
System.out.println(address);
}

public String getMacAddress() throws IOException
{
String macAddress = null;
String command = "ipconfig /all";
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in =
new BufferedReader(
new InputStreamReader(pid.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null)
break;
Pattern p = Pattern.compile(".*Physical Address.*: (.*)");
Matcher m = p.matcher(line);
if (m.matches()) {
macAddress = m.group(1);
break;
}
}
in.close();
return macAddress;
}
}

Use following lines for getting the host使用以下行获取主机

InetAddress address = socket.getInetAddress();
String hostIP = addresss.getHostAddress();

Ashish use this java code and let me know if found any luck. Ashish 使用这个 java 代码,如果找到任何运气,请告诉我。 Also check this link if helpful, How to obtain MAC address of WiFi.network interface?如果有帮助,还要检查此链接, 如何获取 WiFi.network 接口的 MAC 地址?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM