I've a problem getting Mac address using Java. If a machine has VM installed i get different MAC address. Same happens when i try to filter ip address to get its mac while pinging (checking reachabliltiy) of my private network ip address i.e 172.xx.xx.xx
Below is a piece of code i'm trying
public static void main(String args[]) throws IOException {
String loca= System.getProperty("user.home")+File.separatorChar+"Documents"+File.separatorChar+"mac.txt";
FileWriter fw = new FileWriter(loca);
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface interf = interfaces.nextElement();
if (!interf.isLoopback()){ //interf.isUp() && !interf.isLoopback()
Enumeration<InetAddress> mac2 = interf.getInetAddresses();
while(mac2.hasMoreElements()){
String indivIP= mac2.nextElement().getHostAddress();
if(indivIP.contains(".")){
try {
String iptest = "172.x.x.x.x."; //my private network ip
String test2= InetAddress.getLocalHost().getHostName();
System.out.println("HOST NAME = "+test2);
boolean pingStatus = InetAddress.getByName(indivIP).isReachable(3000);
if(pingStatus == true){
System.out.println("IP address = " + indivIP);
byte[] mac = interf.getHardwareAddress();
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("mac address to use = "+sb.toString().toLowerCase());
String macAddress = sb.toString().toLowerCase();
try {
fw.append("\n");
fw.append("IP : " + indivIP);
fw.append("\n");
fw.append("MacAddress: " + macAddress);
fw.append("\n");
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
//future upgrade
}
}
}
}
fw.flush();
fw.close();
}
Other subjects that I've been looking at Get MAC address on local machine with Java
Get MAC Address of System in Java
All I want is to get Ethernet Adapter network.. my physical mac address that comes with hardware nothing more or less.
Below is a sample of my output of ipconfig /all in cmd
C:\***********>ipconfig /all
Windows IP Configuration
Host Name . . . . . . . . . . . . : ***********
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Ethernet adapter Local Area Connection 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : *********** Client Virtual Adapter
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Wireless LAN adapter Local Area Connection* 3:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft Hosted Network Virtual Adapter
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Wireless LAN adapter Local Area Connection* 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter
Physical Address. . . . . . . . . : ((((((((((((((((((( MY REAL MAC ADDRESS)))))))))))))))))))
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : BCM*********** Wireless Network Adapter
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : ***********
IPv4 Address. . . . . . . . . . . : ***********(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : ***********
Lease Expires . . . . . . . . . . : ***********
Default Gateway . . . . . . . . . : ***********
DHCP Server . . . . . . . . . . . : ***********
DHCPv6 IAID . . . . . . . . . . . : ***********
DHCPv6 Client DUID. . . . . . . . : ***********
DNS Servers . . . . . . . . . . . : ***********
NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter Ethernet:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . : ***********
Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller
Physical Address. . . . . . . . . : ((( REAL MAC ADDRESS)))
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Ethernet adapter VirtualBox Host-Only Network:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VirtualBox Host-Only Ethernet Adapter
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : ***********(Preferred)
Autoconfiguration IPv4 Address. . : ***********.77(Preferred)
Subnet Mask . . . . . . . . . . . : ***********
Default Gateway . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : ***********
DHCPv6 Client DUID. . . . . . . . : ***********
DNS Servers . . . . . . . . . . . : ***********
***********
***********
NetBIOS over Tcpip. . . . . . . . : Disabled
Ethernet adapter VMware Network Adapter VMnet1:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet
1
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : ***********
IPv4 Address. . . . . . . . . . . : ***********(Preferred)
Subnet Mask . . . . . . . . . . . :***********
Default Gateway . . . . . . . . . :
DHCP Server . . . . . . . . . . . : ***********
DHCPv6 IAID . . . . . . . . . . . : ***********
DHCPv6 Client DUID. . . . . . . . : ***********
DNS Servers . . . . . . . . . . . : ***********
***********
***********
NetBIOS over Tcpip. . . . . . . . : Disabled
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter for VMnet
8
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : ***********
IPv4 Address. . . . . . . . . . . : ***********(Preferred)
Subnet Mask . . . . . . . . . . . : ***********
Lease Obtained. . . . . . . . . . : ***********
Lease Expires . . . . . . . . . . : ***********
Default Gateway . . . . . . . . . :
DHCP Server . . . . . . . . . . . : ***********
DHCPv6 IAID . . . . . . . . . . . : ***********
DHCPv6 Client DUID. . . . . . . . : ***********
DNS Servers . . . . . . . . . . . : ***********
***********
***********
NetBIOS over Tcpip. . . . . . . . : Disabled
Tunnel adapter isatap.{***********}:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft *********** Adapter
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Tunnel adapter isatap.{***********}:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft *********** Adapter #2
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Tunnel adapter Local Area Connection* 14:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : ***********Interface
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Tunnel adapter isatap.{***********}:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft *********** Adapter #4
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Tunnel adapter isatap.{***********}:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft *********** Adapter #7
Physical Address. . . . . . . . . : ***********
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes