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
- win7下获取局域网内其他win或ubuntu系统电脑的MAC地址
- Dos命令行下,如何通过IP地址获取局域网内机器的MAC地址?
- win7系统查看同一个局域网内电脑IP的方法
- win7系统查看同一个局域网内电脑IP的方法
- 获取局域网内所有网络设备MAC地址
- Win7 无法访问局域网内其他电脑以及共享的文件解决办法,以下错误代码:0x80004005解决办法
- Win10系统下怎么让局域网内其他电脑通过IP访问网站
- Win7环境下,局域网其他电脑访问本机iis网站的设置
- 如何查找局域网内网卡的MAC地址。
- 查找局域网内的mac地址方法
- 通过 ARP 协议获取局域网内指定 IP 地址的机器的 MAC 地址
- 获取电脑的MAC地址
- php 获取局域网的 所有用户的电脑IP和主机名、及mac地址
- 解决win7打开网络看不到局域网的其他电脑/计算机
- 在window下获取电脑的MAC地址
- 获取linux系统电脑网卡mac地址
- 如何访问同一局域网内的其他电脑文件
- 在局域网内让其他电脑访问你的网站
- linux socket编程之socket()函数介绍
- 15PB可乐杯CrackMe01分析
- (一)Mac OS 配置Java虚拟机 和 JDK
- Android—如何添加布局的标题栏
- jboss-as-7.1.1.Final 同步前端jsp、js、css
- win7下获取局域网内其他win或ubuntu系统电脑的MAC地址
- C语言中的值传递
- eth0 找不到
- 欢迎使用CSDN-markdown编辑器
- VB中WinSock控件的属性、方法、事件及应用
- GYM 101086 C.Everything(字典树)
- “玲珑杯”ACM比赛 Round #9 题解 (待续)
- jQuery插件开发中$.extend和$.fn.extend辨析
- 安装新旧版本的arm-linux-gcc交叉工具链(基于JZ2440)