Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the ethernet address using Java?

Tags:

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.

like image 209
Frank Krueger Avatar asked Aug 29 '08 04:08

Frank Krueger


People also ask

How do I find the Ip Address of my computer Java?

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.

How do I find the MAC address of my laptop Java?

First, let's get the MAC address for our machine's localhost: InetAddress localHost = InetAddress. getLocalHost(); NetworkInterface ni = NetworkInterface. getByInetAddress(localHost); byte[] hardwareAddress = ni.

Can I get MAC address from http request?

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.

Is Ethernet address a physical address?

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).


1 Answers

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.

like image 200
skolima Avatar answered Sep 30 '22 14:09

skolima