자바 MAC 주소 가 져 오기

8155 단어 자바
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;


/**
* @title: MacAddressUtil 
* @description:  MAC  
* @author:liming
* @date: 2013-5-5   04:42:50
*/
public class MacAddressUtil{
	
	/**
	* @MethodName: getOSName 
	* @description :           . return          :windows,Linux,Unix 
	* @author:liming
	* @date: 2013-5-5   04:43:36
	* @return String
	*/
	public static String getOSName() {
		return System.getProperty("os.name").toLowerCase();
	}
	
	/**
	* @MethodName: getWindowXPMACAddress 
	* @description :   widnowXp   mac  
	* @author:liming
	* @date: 2013-5-5   04:45:12
	* @param execStr
	* @return String
	*/
	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;
	}
	
	/**
	* @MethodName: getWindow7MACAddress 
	* @description :   widnow7   mac  
	* @author:liming
	* @date: 2013-5-5   04:46:56
	* @param execStr
	* @return String
	*/
	public static String getWindow7MACAddress() {
		//    IP  
		InetAddress ia = null;
		try {
			ia = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		//        (   ),   mac  ,mac       byte   。
        byte[] mac = null;
		try {
			mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
		} catch (SocketException e) {
			e.printStackTrace();
		}
        //      mac     String
        StringBuffer sb = new StringBuffer(); 
        for(int i=0;i<mac.length;i++){
            if(i!=0){
                sb.append("-");
            }
            //mac[i] & 0xFF     byte      
            String s = Integer.toHexString(mac[i] & 0xFF);
            sb.append(s.length()==1?0+s:s);
        }
        //                   mac     
        return sb.toString().toUpperCase();
	}
	
	/**
	* @MethodName: getLinuxMACAddress 
	* @description :   Linux   mac  
	* @author:liming
	* @date: 2013-5-5   04:49:13
	* @return String
	*/
	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;
	}
	
	/**
	* @MethodName: getUnixMACAddress 
	* @description :   Unix   mac  
	* @author:liming
	* @date: 2013-5-5   04:49:59
	* @return String
	*/
	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;
	}
	
	/**
	* @MethodName: getMacAddress 
	* @description :  MAC  
	* @author:liming
	* @date: 2013-5-5   04:53:25
	* @return String
	*/
	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(); 
			}
		}else if (os.startsWith("linux")) {
			mac = getLinuxMACAddress();
		}else{
			mac = getUnixMACAddress();
		}
		return mac;
	}
	
	/**
	* @MethodName: getSystemRoot 
	* @description :jdk1.4         :SystemRoot=C:\WINDOWS
	* @author:liming
	* @date: 2013-5-5   04:56:27
	* @return String
	*/
	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) {
				line=line.toLowerCase();//  (                  ,     .           )
				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;
	}
	

}

좋은 웹페이지 즐겨찾기