java(jdk1.4、jdk1.6)获取mac地址的方法(winxp、win2003、win7、linux)
来源:互联网 发布:网络电视cctv5 编辑:程序博客网 时间:2024/05/01 17:53
项目是年代久远的了,用的是jdk1.4,登录验证需要加mac地址验证,而且是需要兼容主流平台的操作系统。
得,任务落到了咱的头上,啃之。
考虑到jdk1.4没有API取得mac地址,故使用系统命令cmd ipconfig /all 截取mac地址。但考虑到环境变量的改变有可能影响到 ipconfig命令的执行,而ipconfig.exe命令在操作
系统的路径是c:\windows\system32\ipconfig.我们只要获取到环境变量中windir的值,即c:\window
贴上该类,jdk1.4下,在winxp(包括虚拟机)、win2003(包括虚拟机)、win7(联想)均通过,linux和unix没测试。如有错误,欢迎指正。
/** * * @author hyq * jdk1.4下获取mac地址 */public class GetMacAddress {/** * 获取当前操作系统名称. return 操作系统名称 例如:windows,Linux,Unix等. */public static String getOSName() {return System.getProperty("os.name").toLowerCase();} /** * 获取Unix网卡的mac地址. * @return mac地址 */public static String getUnixMACAddress() {String mac = null;BufferedReader bufferedReader = null;Process process = null;try {// Unix下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息process = Runtime.getRuntime().exec("ifconfig eth0");bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line = null;int index = -1;while ((line = bufferedReader.readLine()) != null) {// 寻找标示字符串[hwaddr]index = line.toLowerCase().indexOf("hwaddr");if (index != -1) {// 取出mac地址并去除2边空格mac = line.substring(index + "hwaddr".length() + 1).trim();break;}}} catch (IOException e) {e.printStackTrace();} finally {try {if (bufferedReader != null) {bufferedReader.close();}} catch (IOException e1) {e1.printStackTrace();}bufferedReader = null;process = null;}return mac;}/** * 获取Linux网卡的mac地址 * @return mac地址 */public static String getLinuxMACAddress() {String mac = null;BufferedReader bufferedReader = null;Process process = null;try {// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息process = Runtime.getRuntime().exec("ifconfig eth0");bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line = null;int index = -1;while ((line = bufferedReader.readLine()) != null) {index = line.toLowerCase().indexOf("硬件地址");if (index != -1) {// 取出mac地址并去除2边空格mac = line.substring(index + 4).trim();break;}}} catch (IOException e) {e.printStackTrace();} finally {try {if (bufferedReader != null) {bufferedReader.close();}} catch (IOException e1) {e1.printStackTrace();}bufferedReader = null;process = null;}return mac;}/** * 获取widnowXp网卡的mac地址. * @return mac地址 */public static String getWindowXPMACAddress(String execStr) {String mac = null;BufferedReader bufferedReader = null;Process process = null;try {// windows下的命令,显示信息中包含有mac地址信息process = Runtime.getRuntime().exec(execStr);bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line = null;int index = -1;while ((line = bufferedReader.readLine()) != null) {if(line.indexOf("本地连接") != -1) //排除有虚拟网卡的情况continue;// 寻找标示字符串[physical address]index = line.toLowerCase().indexOf("physical address");if (index != -1) {index = line.indexOf(":");if (index != -1) {//取出mac地址并去除2边空格mac = line.substring(index + 1).trim();}break;}}} catch (IOException e) {e.printStackTrace();} finally {try {if (bufferedReader != null) {bufferedReader.close();}} catch (IOException e1) {e1.printStackTrace();}bufferedReader = null;process = null;}return mac;}/** * 获取widnow7网卡的mac地址. * @return mac地址 */public static String getWindow7MACAddress(String execStr) {String mac = null;BufferedReader bufferedReader = null;Process process = null;try {process = Runtime.getRuntime().exec(execStr);bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));String line = null;int index = -1;while ((line = bufferedReader.readLine()) != null) {index = line.toLowerCase().indexOf("物理地址");if (index != -1) {index = line.indexOf(":");if (index != -1) {mac = line.substring(index + 1).trim();}break;}}} catch (IOException e) {e.printStackTrace();} finally {try {if (bufferedReader != null) {bufferedReader.close();}} catch (IOException e1) {e1.printStackTrace();}bufferedReader = null;process = null;}return mac;}/** * 获取MAC地址 */public static String getMacAddress(){String os = getOSName();String execStr = getSystemRoot()+"/system32/ipconfig /all";String mac = null;if (os.startsWith("windows")) {if(os.equals("windows xp")){ //xpmac = getWindowXPMACAddress(execStr); }else if(os.equals("windows 2003")){ //2003mac = getWindowXPMACAddress(execStr); }else{ //win7mac = getWindow7MACAddress(execStr); //B8-70-F4-49-2B-EE}} else if (os.startsWith("linux")) {mac = getLinuxMACAddress();} else {mac = getUnixMACAddress();}return mac;}/** * jdk1.4获取系统命令路径 :SystemRoot=C:\WINDOWS * @return */public static String getSystemRoot(){String cmd = null;String os = null;String result = null;String envName = "windir";os = System.getProperty("os.name").toLowerCase();if (os.startsWith("windows")) {cmd = "cmd /c SET";} else {cmd = "env";}try {Process p = Runtime.getRuntime().exec(cmd);InputStreamReader isr = new InputStreamReader(p.getInputStream());BufferedReader commandResult = new BufferedReader(isr);String line = null;while ((line = commandResult.readLine()) != null) {if (line.indexOf(envName) > -1) {result = line.substring(line.indexOf(envName)+ envName.length() + 1);return result;}}} catch (Exception e) {System.out.println("获取系统命令路径 error: " + cmd + ":" + e);}return null;}public static void main(String[] args) {System.out.println("mac地址为: "+getMacAddress());}}
再附上一个jdk1.6获取mac的方法,但这个方法没进行过不同操作系统的测试。
/** * jdk1.6下API获取mac地址 * @return */public static String getMAC() {Enumeration<NetworkInterface> el;String mac_s = "";try {el = NetworkInterface.getNetworkInterfaces();while (el.hasMoreElements()) {byte[] mac = el.nextElement().getHardwareAddress();if (mac == null)continue;mac_s = hexByte(mac[0]) + "-" + hexByte(mac[1]) + "-"+ hexByte(mac[2]) + "-" + hexByte(mac[3]) + "-"+ hexByte(mac[4]) + "-" + hexByte(mac[5]);System.out.println(mac_s);}} catch (SocketException e1) {e1.printStackTrace();}return mac_s;}
- java(jdk1.4、jdk1.6)获取mac地址的方法(winxp、win2003、win7、linux)
- (转)jdk1.6下取得mac地址
- WinXP改MAC地址方法(ZT)
- Linux操作系统中安装JAVA JDK1.6的方法
- Linux下安装JDK1.6的方法
- Linux下安装JDK1.6的方法
- Linux下安装JDK1.6的方法
- mac jdk1.6 的安装
- jdk1.4的下载地址
- JDK1.6 获取操作系统IP地址及对应物理mac地址
- win7 32位设置JDK1.6环境变量的方法
- Java---方法引用(JDK1.8)
- jdk1.4与jdk1.5及jdk1.6的区别
- jdk1.4,jdk1.5和jdk1.6的区别
- JDK1.4与JDK1.5、JDK1.6的区别
- jdk1.4,jdk1.5和jdk1.6的区别-笔记
- jdk1.4,jdk1.5和jdk1.6的区别
- jdk1.4与jdk1.5及jdk1.6的区别
- Linux信号机制之sigaction结构体浅析
- 兼容IE和FF的js脚本做法(比较常用)
- Android应用开发之(View)
- 2013年十大IT战略性技术
- apk 签名 混淆
- java(jdk1.4、jdk1.6)获取mac地址的方法(winxp、win2003、win7、linux)
- IOComp组件在BCB中的应用
- 如何提高Bug敏感度
- JS 判断浏览器客户端类型(ipad,iphone,android)
- Android SQLite总结[转载]
- 中国特色的自创英文单词[中英对照]
- Ubuntu12.04 搭建TFTP服务
- 串口转以太网模块--WIZ108SR 用户手册(一)
- 四大精刚