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

方法一:

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"));
}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值