2013-05-08 19 views
9

Programı çalıştıran sistemin mac adresini almam gerekiyor. Ama bunu yapamam.Java'da Sistemin MAC Adresini Alın

public class App{ 

     public static void main(String[] args){ 

     InetAddress ip; 
     try { 

      ip = InetAddress.getLocalHost(); 
      System.out.println("Current IP address : " + ip.getHostAddress()); 

      NetworkInterface network = NetworkInterface.getByInetAddress(ip); 

      byte[] mac = network.getHardwareAddress(); 

      System.out.print("Current MAC address : "); 

      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < mac.length; i++) { 
       sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));   
      } 
      System.out.println(sb.toString()); 

     } catch (UnknownHostException e) { 

      e.printStackTrace(); 

     } catch (SocketException e){ 

      e.printStackTrace(); 

     } 

     } 

    } 

Ve aşağıdaki sonuçlar alıyorum:

kod aşağıdaki yazıyorum

Current IP address : 14.96.192.202 
Current MAC address : 

Ben MAC bunu Adres almıyorum boştur. Sonra birden arayüzleri için hesap varsa

Ben here

+0

Çıktıda NULL gösteriliyor ..! –

+0

Windows veya Linux? Programın yönetici olarak çalıştığından emin olunabilir mi? –

+0

Windows –

cevap

35

gelen bu örneği gördük ve bazı boş MAC adresleri (VMWare yüklü ben Windows 7 üzerinde Java 7 çalıştırıyorum (yani bazı sanal ağ adaptörleri) var) bunu çalıştırdığınızda

İşte
public static void main(String[] args) { 
    try { 
    InetAddress ip = InetAddress.getLocalHost(); 
    System.out.println("Current IP address : " + ip.getHostAddress()); 

    Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces(); 
    while(networks.hasMoreElements()) { 
     NetworkInterface network = networks.nextElement(); 
     byte[] mac = network.getHardwareAddress(); 

     if(mac != null) { 
     System.out.print("Current MAC address : "); 

     StringBuilder sb = new StringBuilder(); 
     for (int i = 0; i < mac.length; i++) { 
      sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); 
     } 
     System.out.println(sb.toString()); 
     } 
    } 
    } catch (UnknownHostException e) { 
    e.printStackTrace(); 
    } catch (SocketException e){ 
    e.printStackTrace(); 
    } 
} 

Bilgisayarımda gördüklerinizi (bir ayıklanmış versiyonu) 'dir: Bu kod iş gibi görünüyor

Current IP address : {I'm not telling :)} 
Current MAC address : 
Current MAC address : {actual hardware interface} 
Current MAC address : 00-00-00-00-00-00-00-E0 
Current MAC address : 00-00-00-00-00-00-00-E0 
Current MAC address : 00-00-00-00-00-00-00-E0 
Current MAC address : 00-00-00-00-00-00-00-E0 
Current MAC address : 00-00-00-00-00-00-00-E0 
Current MAC address : 00-50-56-C0-00-01 
Current MAC address : 00-50-56-C0-00-08 

Ve burada InetAddress.getLocalHost().getHostAddress() ve ağ arayüzleri kendileri arasındaki ilişki geliyor bu karmaşa "sadece bir değer" almakla ipconfig /all

C:\>ipconfig /all 

Windows IP Configuration (minus any actual interface because I don't like sharing that kind of information :) 

Ethernet adapter Local Area Connection 2: 

    Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Gbridge Virtual Private Network Adapter 
    Physical Address. . . . . . . . . : 02-50-F2-CE-82-01 
    DHCP Enabled. . . . . . . . . . . : Yes 
    Autoconfiguration Enabled . . . . : Yes 

Ethernet adapter VMware Network Adapter VMnet1: 

    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet1 
    Physical Address. . . . . . . . . : 00-50-56-C0-00-01 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 
    Link-local IPv6 Address . . . . . : fe80::9c27:3d03:da2c:f14d%19(Preferred) 
    Autoconfiguration IPv4 Address. . : {Hidden}(Preferred) 
    Subnet Mask . . . . . . . . . . . : 255.255.0.0 
    Default Gateway . . . . . . . . . : 
    DHCPv6 IAID . . . . . . . . . . . : 268456022 
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-13-DB-D9-CB-B8-AC-6F-AF-9D-F2 
    DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1 
             fec0:0:0:ffff::2%1 
             fec0:0:0:ffff::3%1 
    NetBIOS over Tcpip. . . . . . . . : Enabled 

Ethernet adapter VMware Network Adapter VMnet8: 

    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet8 
    Physical Address. . . . . . . . . : 00-50-56-C0-00-08 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 
    Link-local IPv6 Address . . . . . : fe80::cdbb:434:7fd9:2574%20(Preferred) 
    IPv4 Address. . . . . . . . . . . : 192.168.42.1(Preferred) 
    Subnet Mask . . . . . . . . . . . : 255.255.255.0 
    Default Gateway . . . . . . . . . : 
    DHCPv6 IAID . . . . . . . . . . . : 285233238 
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-13-DB-D9-CB-B8-AC-6F-AF-9D-F2 
    DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1 
             fec0:0:0:ffff::2%1 
             fec0:0:0:ffff::3%1 
    NetBIOS over Tcpip. . . . . . . . : Enabled 

Tunnel adapter 6TO4 Adapter: 

    Description . . . . . . . . . . . : Microsoft 6to4 Adapter 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 
    IPv6 Address. . . . . . . . . . . : 2002:204c:1bc6::204c:1bc6(Preferred) 
    Default Gateway . . . . . . . . . : 
    NetBIOS over Tcpip. . . . . . . . : Disabled 

Tunnel adapter isatap.{3E45CB42-BC1E-4F89-9C16-25166C0EABA1}: 

    Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 

Tunnel adapter isatap.{77F1FADC-02BA-44AF-9FDF-97E23F8B5FE7}: 

    Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter #3 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 

Tunnel adapter isatap.{608257AC-C0F3-43A5-8595-898533C95D90}: 

    Media State . . . . . . . . . . . : Media disconnected 
    Connection-specific DNS Suffix . : 
    Description . . . . . . . . . . . : Microsoft ISATAP Adapter #5 
    Physical Address. . . . . . . . . : 00-00-00-00-00-00-00-E0 
    DHCP Enabled. . . . . . . . . . . : No 
    Autoconfiguration Enabled . . . . : Yes 

zorluk çalışan çıkışıdır. (Bu gerçekten harika IterableEnumeration yardımcı sınıf kullanarak) ilk programın bu hafif genişlemiş versiyonunu düşünün: Bilgisayarınızda bu çalıştırırsanız

public static void main(String[] args) throws UnknownHostException, SocketException { 
    System.out.println("Current IP address : " + InetAddress.getLocalHost().getHostAddress()); 

    for(NetworkInterface network : IterableEnumeration.make(NetworkInterface.getNetworkInterfaces())) { 
    byte[] mac = network.getHardwareAddress(); 
    if(mac != null) { 
     System.out.print("Current MAC address : "); 
     StringBuilder sb = new StringBuilder(); 
     for (int i = 0; i < mac.length; i++) { 
     sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); 
     } 
     System.out.println(sb.toString()); 
     //Bound InetAddress for interface 
     for(InetAddress address : IterableEnumeration.make(network.getInetAddresses())) { 
     System.out.println("\tBound to:"+address.getHostAddress()); 
     } 
    } 
    } 
} 

Eğer mevcut bir IP adresinin bir adaptöre ilişkilidir ancak MAC adresine göreceksiniz senin donanım arayüzü, kendini farklı bir IP adresi olarak görüyor. Belki sanal adresleri filtreleyebilir veya sadece mevcut tüm adresleri kullanmanın bir yolunu bulabilirsiniz.

+0

kullanıyorum, bir değerler kümesini döndürür. ama programım için tek bir Değere ihtiyacım var. O zaman nasıl sadece bir değer alırdım. ve bu çok uygun. –

+0

Cevabımı ekleyerek bir bıçak attım. –