Get MAC Address of System in Java -
this question has answer here:
- get mac address on local machine java 6 answers
i need mac address of system running program. not able that.
i writing following code:
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(); (int = 0; < 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(); } } }
and getting following results:
current ip address : 14.96.192.202 current mac address :
i not getting mac address blank.
i saw example here
if account multiple interfaces, , null mac addresses (i'm running java 7 on windows 7 vmware installed (so have virtual network adapters)) code seems work:
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(); (int = 0; < 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(); } }
here (a sanitized version of) see on computer when run it:
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
and here output of running ipconfig /all
c:\>ipconfig /all windows ip configuration (minus actual interface because don't sharing 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 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 on tcpip. . . . . . . . : enabled ethernet adapter vmware network adapter vmnet8: connection-specific dns suffix . : description . . . . . . . . . . . : vmware virtual ethernet adapter 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 on 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 on 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
the challenge in getting "just 1 value" mess comes relationship between inetaddress.getlocalhost().gethostaddress()
, network interfaces themselves. consider slight expanded version of initial program (using cool iterableenumeration utility class):
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(); (int = 0; < mac.length; i++) { sb.append(string.format("%02x%s", mac[i], (i < mac.length - 1) ? "-" : "")); } system.out.println(sb.tostring()); //bound inetaddress interface for(inetaddress address : iterableenumeration.make(network.getinetaddresses())) { system.out.println("\tbound to:"+address.gethostaddress()); } } } }
if run on computer see current ip address correlates 1 adapter mac address of hardware interface sees it's self different ip address. maybe can filter out virtual addresses or find way utilize available addresses.
Comments
Post a Comment