簡體   English   中英

如何獲取java中local.network上機器的MAC地址

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

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

提前致謝。

試試這段代碼,

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;
}
}

使用以下行獲取主機

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

Ashish 使用這個 java 代碼,如果找到任何運氣,請告訴我。 如果有幫助,還要檢查此鏈接, 如何獲取 WiFi.network 接口的 MAC 地址?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM