java程序获取网卡地址
来源:互联网 发布:徐州公交查询软件 编辑:程序博客网 时间:2024/06/03 02:33
package com.pure.util;
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class MacAddress {
private static final String[] windowsCommand = { "ipconfig", "/all" };
private static final String[] linuxCommand = { "/sbin/ifconfig", "-a" };
private static final Pattern macPattern = Pattern.compile(".*((:?[0-9a-f]{2}[-:]){5}[0-9a-f]{2}).*",Pattern.CASE_INSENSITIVE);
private final static List<String> getMacAddressList() throws IOException {
final ArrayList<String> macAddressList = new ArrayList<String>();
final String os = System.getProperty("os.name");
final String[] command;
if (os.startsWith("Windows")) {
command = windowsCommand;
} else if (os.startsWith("Linux")) {
command = linuxCommand;
} else {
throw new IOException("Unknown operating system: " + os);
}
final Process process = Runtime.getRuntime().exec(command);
// Discard the stderr
new Thread() {
public void run() {
try {
InputStream errorStream = process.getErrorStream();
while (errorStream.read() != -1) {}
errorStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
// Extract the MAC addresses from stdout
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
for (String line = null; (line = reader.readLine()) != null;) {
Matcher matcher = macPattern.matcher(line);
if (matcher.matches()) {
//macAddressList.add(matcher.group(1));
macAddressList.add(matcher.group(1).replaceAll("[-:]", ""));
}
}
reader.close();
return macAddressList;
}
public static String getMacAddress(){
try {
List<String> addressList=getMacAddressList();
StringBuffer sb=new StringBuffer();
for (Iterator<String> iter = addressList.iterator(); iter.hasNext();) {
sb.append(iter.next());
}
return sb.toString();
} catch (IOException e) {
return null;
}
}
public final static void main(String[] args) {
try {
System.out.println(" MAC Address: " + getMacAddress());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
- java程序获取网卡地址
- Java 程序获取系统硬件信息之网卡MAC地址
- java获取服务器端网卡地址
- java获取服务器端网卡地址
- java获取服务器端网卡地址
- java如何获取网卡地址(硬件地址)
- java获取所有网卡和ip地址
- 纯Java代码获取网卡MAC地址
- JAVA获取本机网卡IP地址
- java获取网卡的mac地址
- java获取网卡的mac地址
- java通过ip获取网卡MAC地址
- 获取网卡MAC地址
- 获取网卡MAC地址
- C#获取网卡地址
- 获取网卡Mac地址
- 获取网卡MAC地址
- 获取单个网卡地址
- C++中 Const用法小结(转载)
- 凯明死的可惜,不过不冤
- 春晓(壹拾染)
- 我所喜欢的网络小说
- 十大Ajax框架
- java程序获取网卡地址
- NO6::RegisterClass
- Tomcat启动分析
- 设计页面学到的东西!
- 新手上路系列之玩转倒车泊车[图解]ZT - 薄荷.红茶o(*^^*)o - 网易博客
- asp.net跳转页面的三种方法比较
- C#中的属性类
- java6.0打开浏览器
- Zju 1109 Language of FatMouse 解题报告