[jcifs] nbtscan and mac addresses

Eric Glass eric.glass at gmail.com
Wed Aug 4 17:02:15 GMT 2004


> 
> Is there a way to obtain the IP and MAC
> address with jcifs as well?
> 

This should work; it will print the host's NetBIOS names/IP and MAC
address (if available):


import jcifs.netbios.NbtAddress;

public class NbtProbe {

    public static void main(String[] args) throws Exception {
        NbtAddress[] addresses = NbtAddress.getAllByAddress(args[0]);
        System.out.println();
        System.out.println("NetBIOS Names:");
        for (int i = addresses.length - 1; i >= 0; i--) {
            System.out.println("    " + addresses[i]);
        }
        byte[] mac = NbtAddress.getByName(args[0]).getMacAddress();
        if (mac != null) {
            System.out.println();
            StringBuffer hex = new StringBuffer("0x");
            for (int i = 0; i < mac.length; i++) {
                hex.append(Integer.toHexString((mac[i] >> 4) & 0x0f));
                hex.append(Integer.toHexString(mac[i] & 0x0f));
            }
            System.out.println("MAC Address: " + hex);
        }
    }

}


Note that you may have to configure some of the jCIFS properties;
specifically, to reach hosts on another subnet you'll need to specify
the IP address of a WINS server in "jcifs.netbios.wins".

On a semi-related note, NbtAddress.getHostName() appears to return the
"unclean" name (i.e., getHostName() returns "hostName.name" while
toString() returns "hostName.toString()").  Didn't know if that was
intentional or not.


Eric


More information about the jcifs mailing list