[英]How to get MAC Address without using IP Address in java socket programming
[英]How can I get MAC Address without using the IP in Java?
如何在不知道和使用Java中的IP地址的情況下找到網卡的MAC地址?
您可以使用NetworkInterface.getHardwareAddress
。 您可以使用NetworkInterface.getNetworkInterfaces()
獲取所有網卡的列表。
您可以使用此代碼來了解 MAC 地址。
InetAddress address = InetAddress.getLocalHost();
NetworkInterface nwi = NetworkInterface.getByInetAddress(address);
byte mac[] = nwi.getHardwareAddress();
System.out.println(mac);
您可以使用如下系統命令(如果您在nix
系統上,如果您的操作系統是 windows 以類似的方式使用ipconfig
):
Runtime r = Runtime.getRuntime();
Process p = r.exec("ifconfig -u |grep ether");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null) {
System.out.println(line);
}
b.close();
在我的系統上它給出:
ether 28:37:37:15:3b:31
ether b2:00:10:65:56:41
...
請注意,可能有很多 MAC 地址、藍牙、以太網、WIFI 等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.