根據IP獲取對應的Mac地址,支持win10+Linux
package com.simonjia.util.other; /** * @Author: SimonHu * @Date: 2019/6/13 11:03 * @Description: */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MacAddress { /** * 調用命令 * @param cmd * @return */ public static String callCmd(String[] cmd) { String result = ""; String line = ""; try { Process proc = Runtime.getRuntime().exec(cmd); 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 cmd 第一個命令 * @param another 第二個命令 * @return 第二個命令的執行結果 */ public static String callCmd(String[] cmd, String[] another) { String result = ""; String line = ""; try { 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, String sourceString, final String macSeparator) { String result = ""; int index = sourceString.indexOf(ip); if (index == -1) { index = 0; } sourceString = sourceString.substring(index, sourceString.length() - 1); String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})"; Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(sourceString); while (matcher.find()) { result = matcher.group(1); if (sourceString.indexOf(ip) <= sourceString.lastIndexOf(matcher.group(1))) { break; // 如果有多個IP,只匹配本IP對應的Mac. } } return result; } /** * @param ip 目標ip * @return Mac Address */ public static String getMacInWindows(final String ip) { String result = ""; String[] cmd = { "cmd", "/c", "ping " + ip }; String[] another = { "cmd", "/c", "arp -a" }; String cmdResult = callCmd(cmd, another); result = filterMacAddress(ip, cmdResult, "-"); return result; } /** * @param ip 目標ip * @return Mac Address * */ public static String getMacInLinux(final String ip) { String result = ""; String[] cmd = { "/bin/sh", "-c", "ping " + ip + " -c 2 && arp -a" }; String cmdResult = callCmd(cmd); result = filterMacAddress(ip, cmdResult, ":"); return result; } /** * 獲取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.println(MacAddress.getMacAddress("222.129.19.10")); } }
獲取同一局域網內的所有IP和對應的Mac
package com.simonjia.util.other; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 獲取同一局域網內的所有IP和對應的Mac * @author liuyazhuang * */ public class AllAddress { /** * 獲取統一局域網的所有IP地址 * @return 所有IP地址的List集合 */ public static List<String> getIPs() { List<String> list = new ArrayList<String>(); Runtime r = Runtime.getRuntime(); Process p; try { p = r.exec("arp -a"); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String inline; while ((inline = br.readLine()) != null) { if(!"".equals(inline.trim())){ if (inline.indexOf("---") > -1) { continue; } if(inline.indexOf("Internet") > -1){ continue; } // 有效IP String[] str = inline.split(" {4}"); list.add(str[0]); // System.out.println(inline); } } br.close(); } catch (IOException e) { e.printStackTrace(); } return list; } /** * 獲取同一局域網內的IP和Mac * @return 以IP地址為Key, Mac地址為Value的Map */ public static Map<String, String> getAllIPAndMac(){ Map<String,String> map = new HashMap<String,String>(); Runtime r = Runtime.getRuntime(); Process p; try { p = r.exec("arp -a"); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String inline; while ((inline = br.readLine()) != null) { if(!"".equals(inline.trim())){ if (inline.indexOf("---") > -1) { continue; } if(inline.indexOf("Internet") > -1){ continue; } // 有效IP String[] arr = inline.split(" {4}"); String ip = arr[0].trim(); String mac = "00-00-00-00-00-00"; for(int i = 1; i < arr.length; i ++){ String str = arr[i].trim(); if(stringIsMac(str)){ mac = str; break; } } map.put(ip, mac); } } br.close(); } catch (IOException e) { e.printStackTrace(); } return map; } /** * 根據正則表達式判斷是否為Mac地址 * @param val * @return true:是Mac地址,false:不是Mac地址 */ private static boolean stringIsMac(String val) { String trueMacAddress = "^([0-9a-fA-F]{2})(([/\\s:-][0-9a-fA-F]{2}){5})$"; // 這是真正的MAC地址;正則表達式; return val.matches(trueMacAddress); } /** * 根據IP提取主機名 * @param ips * @return 以IP地址為Key,主機名為Value的Map */ public static Map<String, String> getHostnames(List<String> ips){ Map<String,String> map = new HashMap<String,String>(); System.out.println("正在提取hostname..."); for(String ip : ips){ String command = "ping -a " + ip; Runtime r = Runtime.getRuntime(); Process p; try { p = r.exec(command); BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream())); String inline; while ((inline = br.readLine()) != null) { if(inline.indexOf("[") > -1){ int start = inline.indexOf("Ping "); int end = inline.indexOf("["); String hostname = inline.substring(start+"Ping ".length(),end-1); System.out.println(hostname); map.put(ip,hostname); } } br.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("提取結束!"); return map; } public static void main(String[] args) { System.out.println(getIPs()); System.out.println(getAllIPAndMac()); } }
參考:https://blog.csdn.net/l1028386804/article/details/46049885