로컬 컴퓨터 맥주소를 추출하는 방법입니다.

// ex.1)
import java.net.InetAddress;
import java.net.NetworkInterface;
 
public class Ex01 {
 
    public static void main(String[] args)throws Exception {
         
        InetAddress ip = InetAddress.getLocalHost();
        // 결과 : john-PC/192.168.206.1
        System.out.println(ip);
         
        NetworkInterface mac = NetworkInterface.getByInetAddress(ip);
        // 결과 : name:eth4 (VMware Virtual Ethernet Adapter for VMnet1)
        System.out.println(mac);
 
         
        if(ip != null) {
            byte[] mc = mac.getHardwareAddress();
             
            String macAddress = "";
             
            for (int i = 0; i < mc.length; i++) {
                macAddress += (String.format("%02x", mc[i]) + ":");
            }
             
            // 결과 : 00:50:56:c0:00:01
            System.out.println(
                macAddress.substring(0, macAddress.length()-1)
            );
        }
    }
}	


아래는 결과 화면입니다.


0 댓글