win7下获取局域网内其他win或ubuntu系统电脑的MAC地址

来源:互联网 发布:淘宝哪家护肤品是正品 编辑:程序博客网 时间:2024/04/30 15:55

方法一:

package com.hk.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class ComputerInfo {private static String macAddressStr = null;// 匹配MAC地址的正则表达式private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",Pattern.CASE_INSENSITIVE);// 执行获取mac地址的命令private static List<String> execCommand(String[] command) throws IOException {ArrayList<String> macAddressList = new ArrayList<String>();Process process = Runtime.getRuntime().exec(command);BufferedReader bufReader = new BufferedReader(new InputStreamReader(process.getInputStream()));for (String line = null; (line = bufReader.readLine()) != null;) {Matcher matcher = macPattern.matcher(line);if (matcher.matches()) {macAddressList.add(matcher.group(1));}}process.destroy();bufReader.close();return macAddressList;}private final static List<String> getMacAddressList(String[] command) throws IOException {ArrayList<String> macAddressList = new ArrayList<String>();macAddressList = (ArrayList<String>) execCommand(command);return macAddressList;}// 从mac地址列表中选出需要的mac地址public static String selectMacAddress(String[] command) {macAddressStr = "";if (macAddressStr == null || macAddressStr.equals("")) {try {List<String> macList = getMacAddressList(command);for (Iterator<String> iter = macList.iterator(); iter.hasNext();) {String amac = iter.next();if (!amac.equals("0000000000E0")) {macAddressStr = amac;break;}}} catch (IOException e) {e.printStackTrace();}}return macAddressStr.toUpperCase();}public static String getMacAddress(String ip) {final String os = System.getProperty("os.name");String macAddressStr = null;String[] windowsCommand = new String[3];String[] linuxCommand = new String[3];if (os.startsWith("Windows")) {windowsCommand[0] = "nbtstat";windowsCommand[1] = "-a";windowsCommand[2] = ip;macAddressStr = selectMacAddress(windowsCommand);if (macAddressStr == "") {windowsCommand[0] = "arp";windowsCommand[1] = "-a";windowsCommand[2] = ip;macAddressStr = selectMacAddress(windowsCommand);}} else {linuxCommand[0] = "arp";linuxCommand[1] = "-a";linuxCommand[2] = ip;String str = selectMacAddress(linuxCommand);macAddressStr = str.replaceAll(":", "-");}return macAddressStr;}// 测试public static void main(String[] args) {System.out.println(ComputerInfo.getMacAddress("192.168.0.111"));}}


方法二:

package com.hk.utils;import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.net.UnknownHostException;public class GetLocalMAC {public static String getLocalMac(InetAddress ia) throws SocketException{byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();StringBuffer sb = new StringBuffer("");for(int i=0; i<mac.length; i++){if(i!=0){sb.append("-");}int temp = mac[i]&0xff;String str = Integer.toHexString(temp);if(str.length()==1){sb.append("0" + str);}else{sb.append(str);}}return sb.toString().toUpperCase();}public static void main(String[] args) throws UnknownHostException, SocketException {InetAddress ia = InetAddress.getLocalHost();System.out.println(getLocalMac(ia));}}


方法三:

package com.hk.utils;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern;public class GetMacAddress {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;}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;}public static String filterMacAddress(final String ip, final String sourceString, final String macSeparator) {String result = "";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;}}return result;}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;}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;}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(getMacAddress("127.0.0.1"));}}


0 0