论坛首页 入门技术论坛

java获取本机网卡的MAC地址

浏览 17462 次
精华帖 (2) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-10-20   最后修改:2008-11-26
★★★ 本篇为原创,需要引用转载的朋友请注明:《 http://stephen830.iteye.com/blog/255210 》 谢谢支持! ★★★

软件项目或者产品有时候会用到本机网卡的物理MAC地址,例如结合MAC地址进行登录验证等等,当然这种项目一般在局域网内运行,能相对提高系统的安全性。

下面列出了java读取MAC地址的工具方法,可以读取windows和非windows操作系统(linux,unix)下的MAC地址。如果你的操作系统很特殊的话,可以对方法进行扩充,原理类似的。


/*
 * Created on 2005-6-5
 * Author stephen
 * Email zhoujianqiang AT gmail DOT com
 * CopyRight(C)2005-2008 , All rights reserved.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 与系统相关的一些常用工具方法.
 * 
 * @author stephen
 * @version 1.0.0
 */
public class SystemTool {

	/**
	 * 获取当前操作系统名称.
	 * return 操作系统名称 例如:windows xp,linux 等.
	 */
	public static String getOSName() {
		return System.getProperty("os.name").toLowerCase();
	}
	
	/**
	 * 获取unix网卡的mac地址.
	 * 非windows的系统默认调用本方法获取.如果有特殊系统请继续扩充新的取mac地址方法.
	 * @return mac地址
	 */
	public static String getUnixMACAddress() {
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			process = Runtime.getRuntime().exec("ifconfig eth0");// linux下的命令,一般取eth0作为本地主网卡 显示信息中包含有mac地址信息
			bufferedReader = new BufferedReader(new InputStreamReader(process
					.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				index = line.toLowerCase().indexOf("hwaddr");// 寻找标示字符串[hwaddr]
				if (index >= 0) {// 找到了
					mac = line.substring(index +"hwaddr".length()+ 1).trim();//  取出mac地址并去除2边空格
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			bufferedReader = null;
			process = null;
		}

		return mac;
	}

	/**
	 * 获取widnows网卡的mac地址.
	 * @return mac地址
	 */
	public static String getWindowsMACAddress() {
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			process = Runtime.getRuntime().exec("ipconfig /all");// windows下的命令,显示信息中包含有mac地址信息
			bufferedReader = new BufferedReader(new InputStreamReader(process
					.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				index = line.toLowerCase().indexOf("physical address");// 寻找标示字符串[physical address]
				if (index >= 0) {// 找到了
					index = line.indexOf(":");// 寻找":"的位置
					if (index>=0) {
						mac = line.substring(index + 1).trim();//  取出mac地址并去除2边空格
					}
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			bufferedReader = null;
			process = null;
		}

		return mac;
	}

	/**
	 * 测试用的main方法.
	 * 
	 * @param argc
	 *            运行参数.
	 */
	public static void main(String[] argc) {
		String os = getOSName();
		System.out.println(os);
		if(os.startsWith("windows")){
			//本地是windows
			String mac = getWindowsMACAddress();
			System.out.println(mac);
		}else{
			//本地是非windows系统 一般就是unix
			String mac = getUnixMACAddress();
			System.out.println(mac);
		}
	}
}




分享知识,分享快乐,希望文章能给需要的朋友带来小小的帮助。

   发表时间:2008-10-20  
这招牛,呵呵
0 请登录后投票
   发表时间:2008-10-30  
不错,不错,,支持楼主
0 请登录后投票
   发表时间:2008-10-30  
daiwenting2601 写道

不错,不错,,支持楼主

0 请登录后投票
   发表时间:2009-02-22  
学到知道精通为止
0 请登录后投票
   发表时间:2009-02-27  
呵呵,很有意思。
0 请登录后投票
   发表时间:2009-03-31  
论坛的回复机制很垃圾,很浪费别人的宝贵时间。
楼主的帖子很有意义,收藏了
0 请登录后投票
   发表时间:2009-03-31   最后修改:2009-03-31
搞成数组返回合适点, 特别是笔记本肯定是2个网卡了
另外最好取已连接网卡的物理地址, 比如像我现在是网线连接的,却取了个无线网卡的mac
0 请登录后投票
   发表时间:2009-10-10  
机器上安装了VMware的虚拟机,使用ipconfig /all 指令会显示好几个,最好加上个判断是否是本地连接的语句。
就像下面实际的是00-03-53-77-E1-32,而程序遇到虚拟机的00-50-56-C0-00-08就算获取到了。。

Windows IP Configuration

        Host Name . . . . . . . . . . . . : www-yokli-test
        Primary Dns Suffix  . . . . . . . :
        Node Type . . . . . . . . . . . . : Hybrid
        IP Routing Enabled. . . . . . . . : No
        WINS Proxy Enabled. . . . . . . . : No
Ethernet adapter VMware Network Adapter VMnet8:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for
VMnet8
        Physical Address. . . . . . . . . : 00-50-56-C0-00-08
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.184.1
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . :

Ethernet adapter VMware Network Adapter VMnet1:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for
VMnet1
        Physical Address. . . . . . . . . : 00-50-56-C0-00-01
        Dhcp Enabled. . . . . . . . . . . : No
        IP Address. . . . . . . . . . . . : 192.168.81.1
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . :

Ethernet adapter 本地连接:

        Connection-specific DNS Suffix  . : eddd.com.cn
        Description . . . . . . . . . . . : Broadcom NetLink (TM) Gigabit Ethern
et
        Physical Address. . . . . . . . . : 00-03-53-77-E1-32
        Dhcp Enabled. . . . . . . . . . . : Yes
        Autoconfiguration Enabled . . . . : Yes
        IP Address. . . . . . . . . . . . : 192.168.42.41
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.42.254
        DHCP Server . . . . . . . . . . . : 192.168.42.254
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics