我在java中使用ipconfig/all
命令和正则表达式来查找MAC地址。
我在Physical Address
命令的输出中搜索ipconfig/all
。
但问题是我希望正则表达式适用于不同的语言环境,即它可以找到任何语言环境的物理地址。
提前致谢。
答案 0 :(得分:1)
选项1:
您可以使用这样的正则表达式(英语,法语,西班牙语):
/(Physical Address|Adresse Physique|Direccion fisica)/
稍后检查您正在使用的语言环境,并因此更新您的正则表达式。
选项2:
直接使用Java(JDK 1.6)获取MAC地址
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class MacAddress {
public static void main(String[] args) {
try {
//InetAddress address = InetAddress.getLocalHost();
InetAddress address = InetAddress.getByName("192.168.0.158");
/*
* Get NetworkInterface for the current host and then read the
* hardware address.
*/
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
if (mac != null) {
/*
* Extract each array of mac address and convert it to hexa with the
* following format 08-00-27-DC-4A-9E.
*/
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
} else {
System.out.println("Address doesn't exist or is not accessible.");
}
} else {
System.out.println("Network Interface for the specified address is not found.");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:1)
不是搜索“物理地址”或其他任何本地化版本(每次需要支持新语言时都需要添加本地化版本),而只需编写一个正则表达式来查找MAC解决问题。
由于我们知道典型的MAC地址由六个分组组成,每个分组包含两个十六进制数字,以冒号,句号或短划线分隔,以下正则表达式将执行此操作:
([a-fA-F0-9]{2}[:\-\.]){5}[a-fA-F0-9]{2}
说明: (两个十六进制数字后跟一个冒号:重复5次)(最后两个十六进制数字)
答案 2 :(得分:1)
这是我运行ipconfig /all
时得到的结果:
Adaptér sítě Ethernet Připojení k místní síti:
Přípona DNS podle připojení . . . : example.com
Popis . . . . . . . . . . . . . . : AMD PCNET Family PCI Ethernet Adapter
Fyzická Adresa. . . . . . . . . . : DE-AD-BE-EF-CA-FE
Protokol DHCP povolen . . . . . . : Ano
Automatická konfigurace povolena : Ano
Adresa IP . . . . . . . . . . . . : 192.168.0.158
Maska podsítě . . . . . . . . . . : 255.255.255.0
Výchozí brána . . . . . . . . . . : 192.168.0.1
Server DHCP . . . . . . . . . . . : 192.168.0.1
Servery DNS . . . . . . . . . . . : 192.168.0.1
Primární server WINS. . . . . . . : 192.168.0.1
Zapůjčeno . . . . . . . . . . . . : 9. září 2011 16:05:32
Zápůjčka vyprší . . . . . . . . . : 9. září 2011 20:05:32
正如你所看到的,寻找字符串“Physical Address”是徒劳的,因为没有一个。但请注意,Windows有自己的MAC地址格式 - 用连字符(字母部分大写)分隔每两个十六进制数字。所以,寻找正则表达式:
([0-9A-F]{2}-){5}[0-9A-F]{2}
会为您提供您正在寻找的MAC地址。
警告:许多计算机都有多个网络接口(有线和wifi,各种VPN等),因此输出中可能有多个MAC。
答案 3 :(得分:1)
注意某些计算机,特别是HP / Compaq MAC地址应该可以从
访问Process pcs = Runtime.getRuntime().exec("wmic bios");
如果是BIOS定制的(Largiest Company)那么这个数据应该可以通过使用JNI / JNA(大量的VB / C#脚本)访问
示例:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class MAC_TEST {
private static Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:]"
+ "[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
private static List getWindowsMACAddresses() {
try {
//Process conf = Runtime.getRuntime().exec("wmic bios");//for HP computers
Process conf = Runtime.getRuntime().exec("ipconfig /all");
//Process p = Runtime.getRuntime().exec("wmic bios /all");
BufferedReader input = new BufferedReader(
new InputStreamReader(conf.getInputStream()));
return getMACAddresses(input);
} catch (Exception e) {
System.err.println("Error Reading Windows MAC Address.");
}
return new ArrayList(1);
}
private static List getLinuxMACAddresses() {
try {
Process conf = Runtime.getRuntime().exec("/sbin/ifconfig");
BufferedReader input = new BufferedReader(
new InputStreamReader(conf.getInputStream()));
return getMACAddresses(input);
} catch (Exception e) {
System.err.println("Error Reading Linux MAC Address.");
}
return new ArrayList(1);
}
private static List getHPUXMACAddresses() {
try {
Process conf = Runtime.getRuntime().exec("/etc/lanscan");
BufferedReader input = new BufferedReader(
new InputStreamReader(conf.getInputStream()));
return getMACAddresses(input);
} catch (Exception e) {
System.err.println("Error Reading HPUX MAC Address.");
}
return new ArrayList(1);
}
private static List getSolarisMACAddresses() {
try {
List rtc = new ArrayList(1);
Process conf = Runtime.getRuntime().exec("/usr/sbin/arp "
+ InetAddress.getLocalHost().getHostAddress());
BufferedReader input = new BufferedReader(
new InputStreamReader(conf.getInputStream()));
rtc.addAll(getMACAddresses(input));
input.close();
input = null;
conf = null;
//Solaris reports MAC address without first 0, change the pattern at re-test
macPattern = Pattern.compile("[0-9a-fA-F][-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}"
+ "[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
conf = Runtime.getRuntime().exec("/usr/sbin/arp "
+ InetAddress.getLocalHost().getHostAddress());
input = new BufferedReader(new InputStreamReader(
conf.getInputStream()));
rtc.addAll(getMACAddresses(input));
//Revert pattern
macPattern = Pattern.compile("[0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}[-:][0-9a-fA-F]{2}"
+ "[-:][0-9a-fA-F]{2}[-:][0 -9a-fA-F]{2}[-:][0-9a-fA-F]{2}");
return rtc;
} catch (Exception e) {
System.err.println("Error Reading Solaris MAC Address.");
}
return new ArrayList(1);
}
private static List getMACAddresses(BufferedReader input) throws Exception {
List MACs = new ArrayList(1);
String theLine;
while ((theLine = input.readLine()) != null) {
String[] ss = macPattern.split(theLine);
for (int p = 0; p < ss.length; p++) {
String s = theLine.substring(theLine.indexOf(ss[p]) + ss[p].length()).trim();
if (!s.isEmpty()) {
String s1 = s.replaceAll("-", ":");
String s2 = s1.substring(0, s1.lastIndexOf(':') + 3);
if (s2.length() == 16 || s2.length() == 17) {
MACs.add(s2);
}
}
}
}
return MACs;
}
public static void main(String[] args) {
try {
System.out.println("WINDOWS ... Found the following MAC Addresses: ");
List MACS = getWindowsMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
System.out.println("Linux ... Found the following MAC Addresses: ");
MACS = getLinuxMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
System.out.println("Solaris ... Found the following MAC Addresses: ");
MACS = getSolarisMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
System.out.println("HPUX ... Found the following MAC Addresses: ");
MACS = getHPUXMACAddresses();
System.out.println("*-----------------*");
for (int i = 0; i < MACS.size(); i++) {
System.out.println("|" + MACS.get(i) + "|");
}
System.out.println("*-----------------*");
System.out.println(" ");
} catch (Exception ex) {
ex.printStackTrace();
}
}
private MAC_TEST() {
}
}