Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You can get the address that connects to your ServerSocket using http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html#getInetAddresses()

However if your client is connecting via a NAT, then you will get the address of the router and NOT the Ethernet address. If it is on your local network (via a hub/switch, no router with NAT) the it wil work as intended.

Actually, beyond other right answers (JDK 6; exec 'ifconfig'), there are JNI-based libraries. Java Uuid Generator (JUG) 2.0 has code for some platforms. This works on JDK 1.2 and above at least (maybe 1.1 even)

You can use Jxnet: https://github.com/ardikars/Jxnet (Require libpcap/winpcap)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top