자바, 이 컴퓨터 MAC 주소 와 IP 가 져 오기

7501 단어 자바
자바 에서 windows, Linux, windows 7 의 MAC 주 소 를 가 져 옵 니 다.
http://yuelangyc.iteye.com/blog/1069713
다 중 네트워크 카드 는 문제 가 있 습 니 다. 예 를 들 어 Virsualbox 의 네트워크 카드 를 찾 을 수 있 습 니 다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;

/**
 *               .
 * 
 * @author lvbogun
 * @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 {
			// linux    ,   eth0       
			process = Runtime.getRuntime().exec("ifconfig eth0");
			//         mac    
			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 >= 0) {//    
					//   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;
	}

	/**
	 *   widnows   mac  .
	 * 
	 * @return mac  
	 */
	public static String getWindowsMACAddress() {
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			// windows    ,        mac    
			process = Runtime.getRuntime().exec("ipconfig /all");
			bufferedReader = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				System.out.println(line);
				//        [physical
				index = line.toLowerCase().indexOf("physical address");
				
				if (index >= 0) {//    
					index = line.indexOf(":");//   ":"   
					if (index >= 0) {
						System.out.println(mac);
						//   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;
	}

	/**
	 * windows 7      MAC  
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String getMACAddress() throws Exception {
		
		//     IP  
		InetAddress ia = InetAddress.getLocalHost();
		//         (   ),   mac  ,mac       byte   。
		byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();

		//       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();
	}

	/**
	 *     main  .
	 * 
	 * @param argc     .
	 * @throws Exception
	 */
	public static void main(String[] argc) throws Exception {
		String os = getOSName();
		System.out.println(os);
		if (os.equals("windows 7")) {
			String mac = getMACAddress();
			System.out.println(mac);
		} else if (os.startsWith("windows")) {
			//    windows
			String mac = getWindowsMACAddress();
			System.out.println(mac);
		} else {
			//     windows       unix
			String mac = getUnixMACAddress();
			System.out.println(mac);
		}
	}
}

모든 IP 획득
이 코드 는 컴퓨터 에 있 는 모든 장치 의 ip 을 출력 하여 필요 한 것 을 찾 습 니 다.
http://kaza.iteye.com/blog/169889
Enumeration<NetworkInterface> netInterfaces = null;
try {
	netInterfaces = NetworkInterface.getNetworkInterfaces();
	while (netInterfaces.hasMoreElements()) {
		NetworkInterface ni = netInterfaces.nextElement();
		System.out.println("DisplayName:" + ni.getDisplayName());
		System.out.println("Name:" + ni.getName());
		Enumeration<InetAddress> ips = ni.getInetAddresses();
		while (ips.hasMoreElements()) {
			System.out.println("IP:"
			+ ips.nextElement().getHostAddress());
		}
	}
} catch (Exception e) {
	e.printStackTrace();
}

http://www.cnblogs.com/hxsyl/p/3422191.html
다음 코드 는 다 중 네트워크 카드 에 문제 가 발생 했 습 니 다.
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
/*
 *      48 ,  ipv6   
 */
public class LOCALMAC {
	/**
	 * @param args
	 * @throws UnknownHostException 
	 * @throws SocketException 
	 */
	public static void main(String[] args) throws UnknownHostException, SocketException {
		// TODO Auto-generated method stub
		
		//  IP,  PC-201309011313/122.206.73.83
		InetAddress ia = InetAddress.getLocalHost();
		System.out.println(ia);
		getLocalMac(ia);
	}
	private static void getLocalMac(InetAddress ia) throws SocketException {
		// TODO Auto-generated method stub
		//    ,    
		byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
		System.out.println("mac    :"+mac.length);
		StringBuffer sb = new StringBuffer("");
		for(int i=0; i<mac.length; i++) {
			if(i!=0) {
				sb.append("-");
			}
			//       
			int temp = mac[i]&0xff;
			String str = Integer.toHexString(temp);
			System.out.println(" 8 :"+str);
			if(str.length()==1) {
				sb.append("0"+str);
			}else {
				sb.append(str);
			}
		}
		System.out.println("  MAC  :"+sb.toString().toUpperCase());
	}
}

좋은 웹페이지 즐겨찾기