由於前面提到的找“qa-qd-61-151”主機名稱的問題,所以對Java擷取主機名稱的東西進行了學習,順便瞭解了一下Java擷取MAC地址/IP地址等的方法。
根據參考資料中的兩篇文章,整合了以下,實現了Java擷取MAC地址/IP地址/主機名稱的功能。實現原理如下:
IP地址和主機名稱時直接調用 java.net.InetAddress 這個類中的方法來實現的;而MAC地址,區分Windows和Unix兩種作業系統,分別用"ipconfig /all"和"ifconfig eth0"命令來擷取輸出,然後對其進行解析,找到MAC地址的位置。如果作業系統的語言不同,解析裡面的內容也時有差別的;另外如果網卡不是eth0, 也需要相應的修改,需要根據自己實際應用情境進行修改的。 代碼如下:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.UnknownHostException; /** * @className: SystemTool * @description: 與系統相關的一些常用工具方法. 目前實現的有:擷取MAC地址、IP地址、主機名稱 * @author: 笑遍世界 * @createTime: 2010-11-13 下午08:03:44 */ 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; } /** * @return 本機主機名稱 */ public static String getHostName() { InetAddress ia = null; try { ia = InetAddress.getLocalHost(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (ia == null ) { return "some error.."; } else return ia.getHostName(); } /** * @return 本機IP 位址 */ public static String getIPAddress() { InetAddress ia = null; try { ia = InetAddress.getLocalHost(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (ia == null ) { return "some error.."; } else return ia.getHostAddress(); } /** * 測試用的main方法. * * @param argc * 運行參數. */ public static void main(String[] argc) { String os = getOSName(); System.out.println("OS Type:"+os); if(os.startsWith("windows")){ //本地是windows String mac = getWindowsMACAddress(); System.out.println("MAC Address:"+mac); }else{ //本地是非windows系統 一般就是unix String mac = getUnixMACAddress(); System.out.println(mac); } System.out.println("HostName:"+getHostName()); System.out.println("IPAddress:"+getIPAddress()); } //這個更簡單 Map<String, String> map = System.getenv(); String userName = map.get("USERNAME");// 擷取使用者名稱 String computerName = map.get("COMPUTERNAME");// 擷取電腦名稱 String userDomain = map.get("USERDOMAIN");// 擷取電腦網域名稱 |