How to get uuid or mac address from client in Java?

I am looking for a solution for a Java based web application to uniquely identify a client. The server is on the same network as the clients, and I thought using a MAC address would be a good solution. The problem is that I cannot work with cookies because they can be deleted on the client side and I cannot use IP because they can simply issue a new DHCP lease renewal.

Therefore, I would like to return to the clients MAC address. I know that there is no built-in java function to get the MAC address. Is there a library that can handle the output of each OS? (basic Windows and Mac), as the Java application runs on both platforms.

or are there any other suggestions for the unique identification of the client on the website and the HTTP protocol? (maybe HTML5 datastores or something else)

I am using Java 1.7 btw.

I will not force the user to log in or otherwise identify themselves, and I will not program my own application for client smartphones.

+5
source share
3 answers

I wrote my own method to solve my problem. Here, if ever someone needs a code to find the MAC address on the same network. Works for me without any administrator privileges in Win 7 and Mac OS X 10.8.2

Pattern macpt = null;

private String getMac(String ip) {

    // Find OS and set command according to OS
    String OS = System.getProperty("os.name").toLowerCase();

    String[] cmd;
    if (OS.contains("win")) {
        // Windows
        macpt = Pattern
                .compile("[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+-[0-9a-f]+");
        String[] a = { "arp", "-a", ip };
        cmd = a;
    } else {
        // Mac OS X, Linux
        macpt = Pattern
                .compile("[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+");
        String[] a = { "arp", ip };
        cmd = a;
    }

    try {
        // Run command
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        // read output with BufferedReader
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line = reader.readLine();

        // Loop trough lines
        while (line != null) {
            Matcher m = macpt.matcher(line);

            // when Matcher finds a Line then return it as result
            if (m.find()) {
                System.out.println("Found");
                System.out.println("MAC: " + m.group(0));
                return m.group(0);
            }

            line = reader.readLine();
        }

    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    // Return empty string if no MAC is found
    return "";
}
+11
source

The best I can find is this: ARP request cache to get the MAC ID

And the potty resume is as follows:

  • API Java,
  • ,
  • (, root-) ARP
  • , MAC- .

, .

, :

  • , . .
  • MAC- .
+3

IP- . MAC- - .

public String getMacAddress() throws Exception {
    String macAddress = null;
    String command = "ifconfig";

    String osName = System.getProperty("os.name");
    System.out.println("Operating System is " + osName);

    if (osName.startsWith("Windows")) {
        command = "ipconfig /all";
    } else if (osName.startsWith("Linux") || osName.startsWith("Mac") || osName.startsWith("HP-UX")
            || osName.startsWith("NeXTStep") || osName.startsWith("Solaris") || osName.startsWith("SunOS")
            || osName.startsWith("FreeBSD") || osName.startsWith("NetBSD")) {
        command = "ifconfig -a";
    } else if (osName.startsWith("OpenBSD")) {
        command = "netstat -in";
    } else if (osName.startsWith("IRIX") || osName.startsWith("AIX") || osName.startsWith("Tru64")) {
        command = "netstat -ia";
    } else if (osName.startsWith("Caldera") || osName.startsWith("UnixWare") || osName.startsWith("OpenUNIX")) {
        command = "ndstat";
    } else {// Note: Unsupported system.
        throw new Exception("The current operating system '" + osName + "' is not supported.");
    }

    Process pid = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
    Pattern p = Pattern.compile("([\\w]{1,2}(-|:)){5}[\\w]{1,2}");
    while (true) {
        String line = in.readLine();
        System.out.println("line " + line);
        if (line == null)
            break;

        Matcher m = p.matcher(line);
        if (m.find()) {
            macAddress = m.group();
            break;
        }
    }
    in.close();
    return macAddress;
}

This should work everywhere. At least using this method on an Ubuntu machine gives the following result:

Operating System is Linux
line eth0      Link encap:Ethernet  HWaddr f4:6d:04:63:8e:21  
mac: f4:6d:04:63:8e:21
0
source

All Articles