发布网友 发布时间:2022-04-22 05:39
共1个回答
热心网友 时间:2023-05-08 11:59
以windows举例。\x0d\x0a运行命令" cmd ipconfig /all"就会出现以下结果\x0d\x0a \x0d\x0aPhysical Address. . . . . . . . . : 20-CF-30-9A-60-EE\x0d\x0a。\x0d\x0ajava就能过这样的命令来获取。以下是示例。\x0d\x0a\x0d\x0aimport java.io.BufferedReader;\x0d\x0aimport java.io.IOException;\x0d\x0aimport java.io.InputStreamReader;\x0d\x0a\x0d\x0apublic class TestMac\x0d\x0a{\x0d\x0a public static void main(String[] args) {\x0d\x0a System.out.println("Operation System=" + getOsName());\x0d\x0a System.out.println("Mac Address=" + getMACAddress());\x0d\x0a System.out.println("通过ip获取mac"+getMACAddress("192.168.1.101"));\x0d\x0a }\x0d\x0a\x0d\x0a public static String getOsName() {\x0d\x0a String os = "";\x0d\x0a os = System.getProperty("os.name");\x0d\x0a return os;\x0d\x0a }\x0d\x0a \x0d\x0a public static String getMACAddress() {\x0d\x0a String address = "";\x0d\x0a String os = getOsName();\x0d\x0a if (os.startsWith("Windows")) {\x0d\x0a try {\x0d\x0a String command = "cmd.exe /c ipconfig /all";\x0d\x0a Process p = Runtime.getRuntime().exec(command);\x0d\x0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));\x0d\x0a String line;\x0d\x0a while ((line = br.readLine()) != null) {\x0d\x0a if (line.indexOf("Physical Address") > 0) {\x0d\x0a int index = line.indexOf(":");\x0d\x0a index += 2;\x0d\x0a address = line.substring(index);\x0d\x0a break;\x0d\x0a }\x0d\x0a }\x0d\x0a br.close();\x0d\x0a return address.trim();\x0d\x0a } catch (IOException e) {\x0d\x0a }\x0d\x0a } else if (os.startsWith("Linux")) {\x0d\x0a String command = "/bin/sh -c ifconfig -a";\x0d\x0a Process p;\x0d\x0a try {\x0d\x0a p = Runtime.getRuntime().exec(command);\x0d\x0a BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));\x0d\x0a String line;\x0d\x0a while ((line = br.readLine()) != null) {\x0d\x0a if (line.indexOf("HWaddr") > 0) {\x0d\x0a int index = line.indexOf("HWaddr") + "HWaddr".length();\x0d\x0a address = line.substring(index);\x0d\x0a break;\x0d\x0a }\x0d\x0a }\x0d\x0a br.close();\x0d\x0a } catch (IOException e) {\x0d\x0a }\x0d\x0a }\x0d\x0a address = address.trim();\x0d\x0a return address;\x0d\x0a }\x0d\x0a\x0d\x0apublic static String getMACAddress(String ipAddress) { \x0d\x0aString str = "", strMAC = "", macAddress = ""; \x0d\x0atry { \x0d\x0aProcess pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress); \x0d\x0aInputStreamReader ir = new InputStreamReader(pp.getInputStream()); \x0d\x0aLineNumberReader input = new LineNumberReader(ir); \x0d\x0afor (int i = 1; i < 100; i++) { \x0d\x0astr = input.readLine(); \x0d\x0aif (str != null) { \x0d\x0aif (str.indexOf("MAC Address") > 1) { \x0d\x0astrMAC = str.substring(str.indexOf("MAC Address") + 14, \x0d\x0astr.length()); \x0d\x0abreak; \x0d\x0a} \x0d\x0a} \x0d\x0a} \x0d\x0a} catch (IOException ex) { \x0d\x0areturn "Can't Get MAC Address!"; \x0d\x0a} \x0d\x0a// \x0d\x0aif (strMAC.length() < 17) { \x0d\x0areturn "Error!"; \x0d\x0a} \x0d\x0a\x0d\x0amacAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5) \x0d\x0a+ ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11) \x0d\x0a+ ":" + strMAC.substring(12, 14) + ":" \x0d\x0a+ strMAC.substring(15, 17); \x0d\x0a// \x0d\x0areturn macAddress; \x0d\x0a} \x0d\x0a} \x0d\x0a\x0d\x0a剑天梦的回答原理和我这个一样,都是通过Process 执行命令。 我直接补充到答案里了。不过\x0d\x0a我这边运行那个命令出来的结果很多,那么花的时间就长了。优点是能够获取别人的mac地址 。