java(jdk1.4、jdk1.6)獲取mac地址的方法(winxp、win2003、win7、linux)

項目是年代久遠的了,用的是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")){  //xp
				mac = getWindowXPMACAddress(execStr);  
			}else if(os.equals("windows 2003")){  //2003
				mac = getWindowXPMACAddress(execStr);   
			}else{  //win7
				mac = 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;
	}


 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章