الطريقة الأولى: تمييز عنوان الجهاز المحلي عن الأجهزة الأخرى في الشبكة المحلية (LAN).
/** * احصل على عنوان mac بناءً على عنوان IP * @param ipAddress 127.0.0.1 * @return * @throwsocketException * @throws UnknownHostException */ public String getLocalMac(String ipAddress) throwsocketException, UnknownHostException { // TODO Auto - كعب الطريقة الذي تم إنشاؤه String str = ""; String macAddress = ""; LOOPBACK_ADDRESS = "127.0.0.1"; // إذا كان 127.0.0.1، فاحصل على عنوان MAC المحلي. if (LOOPBACK_ADDRESS.equals(ipAddress)) { InetAddress inetAddress = InetAddress.getLocalHost(); // يبدو أن هذه الطريقة تتطلب JDK1.6. byte[] mac = NetworkInterface.getByInetAddress(inetAddress) .getHardwareAddress(); // الكود التالي يجمع عنوان mac في String StringBuilder sb = new StringBuilder(); { if (i != 0) { sb.append("-" } // mac[i] & 0xFF إنه لتحويل البايت إلى عدد صحيح موجب String s = Integer.toHexString(mac[i] & 0xFF); قم بتغيير الأحرف الصغيرة إلى أحرف كبيرة لتصبح عنوان Mac العادي وإرجاع macAddress = sb.toString().trim().toUpperCase(); return macAddress } else { // احصل على عنوان MAC لعنوان IP غير المحلي حاول { System.out.println(ipAddress); Process p = Runtime.getRuntime() .exec("nbtstat -A " + ipAddress); ("= ==process=="+p); InputStreamReader ir = new InputStreamReader(p.getInputStream()); while ((str = br.readLine()) != null) { if(str.indexOf("MAC")>1){ macAddress = str.substring(str.indexOf("MAC")+9, str. length ()); macAddress = macAddress.trim(); System.out.println("macAddress:" + macAddress); ir.Close(); } Catch (IOException ex) { } return macAddress;
دعونا نلقي نظرة على الطريقة الثانية:
الحزمة com.alpha.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.LineNumberReader;فئة عامة GetMac {/** * تحصل Java على عنوان MAC الخاص ببطاقة شبكة العميل * * @param args */public static void main(String[] args) {GetMac get = new GetMac();System.out.println("1="+get.getMAC());System.out.println("2="+get.getMAC("127.0.0.1"));}// 1 .احصل على عنوان IP للعميل (يجب تمريره من العميل إلى الخلفية): // ضمن صفحة jsp، الأمر بسيط جدًا، request.getRemoteAddr();// نظرًا لأنه يتم تنفيذ الطبقة VIew من النظام باستخدام JSF، لا يمكن الحصول على طلبات مماثلة مباشرة على الصفحة، لذلك يتم إجراء تحويل قسري في الحبة // public String getMyIP() {// حاول {// FacesContext fc = FacesContext. getCurrentInstance ();// طلب HttpServletRequest = (HttpServletRequest) fc// .getExternalContext().getRequest();// return request.getRemoteAddr();// } Catch (Exception e) {// e.printStackTrace();// }// return "";// }// 2. احصل على عنوان mac للعميل // اتصل بأمر النافذة ، في الخلفية Bean، احصل على عنوان mac من خلال IP. الطريقة هي كما يلي: // سرعة التشغيل [سريعة] public String getMAC() {String mac = null حاول {Process pro = Runtime.getRuntime().exec("cmd.exe /c ipconfig/all");InputStream is = pro .getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));String message = br.readLine();int Index = -1;while (message != null) {if ((index = message.indexOf("العنوان الفعلي")) > 0) {mac = message.substring(index + 36).trim();break;}message = br.readLine();}System.out.println(mac);br. Close();pro.destroy();} الصيد (IOException e) {System.out.println("لا يمكن الحصول على عنوان mac!");return null;}return mac;}// سرعة التشغيل [بطيئة] public String getMAC(String ip) {String str = null;String macAddress = null ;جرب {Process p = Runtime.getRuntime().exec("nbtstat -A" + ip);InputStreamReader ir = new InputStreamReader(p.getInputStream());LineNumberReader input = new LineNumberReader(ir);for (; true;) {str = input.readLine();if (str != null) {if (str.indexOf("عنوان MAC" ") > 1) {macAddress = str.substring(str.indexOf("عنوان MAC") + 14);break;}}}} الصيد (IOException e) {e.printStackTrace(System.out);return null;}return macAddress;}}
الطريقة الثالثة، أكثر بساطة
import java.io.IOException;import java.io.InputStreamReader;import java.io.LineNumberReader;public class MACAddress { String ip; public MACAddress (String ip){ this.ip = ip} public MACAddress (){; this.ip = "0.0.0.0" } public String getMac(){ حاول { العملية p = Runtime.getRuntime().exec("arp -n"); InputStreamReader ir = new InputStreamReader(p.getInputStream()); LineNumberReader input = new LineNumberReader(ir); = "(" + this.ip + ")"; while(flag) { String str = input.readLine(); null) { if (str.indexOf(ipStr) > 1) { int temp = str.indexOf("at"); this.mac = str.substring( temp + 3, temp + 20); = false } }catch (IOException | InterruptedException e) { e.printStackTrace(System.out) } return this.mac } public void setIp(String ip){ this.ip = ip }}
أخيرًا، علينا أن نوسع الحركة أيها الأصدقاء، راقبوا بعناية.
أول ما يجب قوله هو: يمكن أن تدعم هذه الطريقة الحصول على عنوان mac لجهاز الشبكة الخارجية.
اعتدت أن أمتلك واحدًا يمكنه الوصول فقط إلى الشبكة المحلية (LAN). لا يمكنك الوصول إليه إذا كان لديك جدار حماية، ولكن لا داعي للقلق بشأن ذلك.
لقد اختبرت عنوان IP الخاص بشركة Baidu ويمكنني الحصول على عنوان mac.
تحصل Java على عنوان mac من خلال عنوان IP - block ip block mac
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** احصل على عنوان MAC* @author* 2011-12*/public class GetMacAddress { public static String callCmd(String[] cmd) { String result = ""; String line = ""; حاول { عملية proc = Runtime.getRuntime().exec(cmd); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader (is); ((line = br.readLine ()) != null) { result += line; } } Catch(Exception e) { e.printStackTrace(); } return result } /** * * @param الأمر الأول cmd* @param أمر ثانٍ آخر* @return نتيجة تنفيذ الأمر الثاني*/ public static String callCmd(String[] cmd,String[] else) { String result = ""; String line = ""; Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(cmd); proc.waitFor(); // تم تنفيذ الأمر الأول وتم تنفيذ الأمر الثاني proc = rt.exec(another); InputStreamReader is = new InputStreamReader(proc.getInputStream()); BufferedReader br = new BufferedReader(is); while ((line = br.readLine ()) != null) { result += line; } } Catch(Exception e) { e.printStackTrace() } return result } /** * * @param ip عنوان IP المستهدف، عادةً داخل الشبكة المحلية* @param sourceString سلسلة نتائج معالجة الأوامر*param macSeparator mac رمز الفاصل* @return عنوان mac، يمثله رمز الفاصل أعلاه*/ public static String filterMacAddress(final String ip, Final String sourceString ,final String macSeparator) { String result = ""; String regExp = "(([0-9,AF,af]{1,2}" + macSeparator + "){1,5})[0-9,AF,af]{1,2})"; Pattern Pattern = Pattern.compile(regExp); Matcher = Pattern.matcher(sourceString); ()){ result = matcher.group(1); if(sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) {break; // إذا كان هناك عناوين IP متعددة، فسيتم مطابقة جهاز Mac المتوافق مع عنوان IP هذا فقط. } } return result } /** * * @param ip target ip * @return Mac Address * */ public static String getMacInWindows(final String ip) ){ نتيجة السلسلة = ""; String[] cmd = { "cmd", "/c", "ping " + ip }; }؛ خيط cmdResult = callCmd(cmd,another); result = filterMacAddress(ip,cmdResult,"-"); return result } /** * @param ip target ip * @return Mac Address * */ public static String getMacInLinux(final String); ip){ نتيجة السلسلة = ""; String[] cmd = { "/bin/sh", "-c", "ping " + ip + " -c 2 && arp -a" }; String cmdResult = callCmd(cmd); result = filterMacAddress(ip,cmdResult,":":); إرجاع النتيجة; } /** * احصل على عنوان MAC* @return قم بإرجاع عنوان MAC*/ public static String getMacAddress(String ip){ String macAddress = ""; macAddress = getMacInWindows(ip).trim(); if(macAddress==null||"".equals(macAddress)){ macAddress = getMacInLinux(ip).trim(); } return macAddress } // قم بإجراء اختبار public static void main(String[] args) { System.out .out.println(getMacAddress("220.181.111.148"));