2012年3月5日星期一

MAC address lookup using Java

MAC address lookup using Java
Posted 2007-07-09 in Java.
Code to extract the MAC address of the network card has been in UUID for a long time now. Because some people download UUID for just the MAC address part, here is a short explanation of how that code works and how you can use it in your projects.
First, download UUID and open com.eaio.uuid.UUIDGen. The MAC address extraction is performed in the static initializer.
The code does a little bit of OS sniffing to find a suitable program which is then used to print the MAC address. The output of this program is read in and parsed.
Operating systems
HP-UX
On HP-UX, /usr/sbin/lanscan is started.
Linux/MacOSX/possibly other Unices
The /sbin/ifconfig command is used.
Windows
ipconfig /all is used.
Solaris
On Solaris, the first line of the host name list is used to call /usr/sbin/arp. The code is somewhat equivalent to /usr/sbin/arp `uname -n | head -1`.
Ripping the MAC code
    * Download the latest version of the UUID software and extract the archive.
    * Copy the following source files from the uuid-x.x.x/src/java folder into your project:
          o com/eaio/util/lang/Hex.java
          o com/eaio/uuid/MACAddressParser.java
          o com/eaio/uuid/UUIDGen.java
    * Copy the static initializer up to and including
      if (macAddress != null) {
          if (macAddress.indexOf(':') != -1) {
              clockSeqAndNode |= Hex.parseLong(macAddress);
          }
          else if (macAddress.startsWith("0x")) {
              clockSeqAndNode |= Hex.parseLong(macAddress.substring(2));
          }
      }
      into a new class.
    * Retrieve the MAC address as clockSeqAndNode variable.
That's all.

没有评论:

发表评论