特定の Web サイトへのアクセスに使用されるネットワーク インターフェイスのイーサネット アドレスを取得したいと考えています。
Javaでこれを行うにはどうすればよいですか?
ソリューションの承認されたソリューションはgetHardwareAddress
、Java 6 でのみ利用可能であることに注意してください。i(f|p)confing を実行する以外に、Java 5 のソリューションはないようです。
特定の Web サイトへのアクセスに使用されるネットワーク インターフェイスのイーサネット アドレスを取得したいと考えています。
Javaでこれを行うにはどうすればよいですか?
ソリューションの承認されたソリューションはgetHardwareAddress
、Java 6 でのみ利用可能であることに注意してください。i(f|p)confing を実行する以外に、Java 5 のソリューションはないようです。
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.
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.
実際、他の正解 (JDK 6; exec 'ifconfig') 以外にも、JNI ベースのライブラリがあります。Java Uuid Generator (JUG) 2.0には、一部のプラットフォーム用のコードが含まれています。これは、少なくとも JDK 1.2 以降 (おそらく 1.1 でも) で動作します。