How do you get the ethernet address using Java?

ぃ、小莉子 提交于 2019-11-29 10:57:42

问题


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.


回答1:


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.




回答2:


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.




回答3:


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)




回答4:


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



来源:https://stackoverflow.com/questions/33960/how-do-you-get-the-ethernet-address-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!