MAC address lookup using Java
Posted 2007-07-09 in Java by Johann.
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:com/eaio/util/lang/Hex.java
com/eaio/uuid/MACAddressParser.java
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.
Subscribe
RSS 2.0, Atom or subscribe by Email.
Top Posts
- DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
- 6 fast jQuery Tips: More basic Snippets
- xslt.js version 3.2 released
- xslt.js version 3.0 released XML XSLT now with jQuery plugin
- Forum Scanners - prevent forum abuse
- Automate JavaScript compression with YUI Compressor and /packer/