I would like to retrieve the ethernet address of the network interface that is used to access a particular website.
How can this be done in Java?
Solution Note that the accepted solution of getHardwareAddress
is only available in Java 6. There does not seem to be a solution for Java 5 aside from executing i(f|p)confing.
In Java, you can use InetAddress. getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress. getHostName() to get Hostname of the current Server name.
First, let's get the MAC address for our machine's localhost: InetAddress localHost = InetAddress. getLocalHost(); NetworkInterface ni = NetworkInterface. getByInetAddress(localHost); byte[] hardwareAddress = ni.
MAC is a property of a TCP packet, and on HTTP level there're no packets or MACs (for example, a single HTTP request might be assembled of several TCP packets). You could try using a packet sniffer (like WireShark) to capture TCP packets, and then analyze them to extract MACs and map them to HTTP requests.
In a LAN, each node is assigned a physical address, also known as a MAC/Ethernet address. This address is unique to each of the nodes on the LAN and is 6 bytes (48 bits) long, which is burned on the Ethernet card (also known as the network interface card).
java.net.NetworkInterface.getHardwareAddress (method added in Java 6)
It has to be called on the machine you are interested in - the MAC is not transferred across network boundaries (i.e. LAN and WAN). If you want to make use of it on a website server to interrogate the clients, you'd have to run an applet that would report the result back to you.
For Java 5 and older I found code parsing output of command line tools on various systems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With