Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm looking for a solution for a Java based webapplication to uniquely identify the client. The server is in the same network as the clients and I thought that using the MAC address would be a good solution. The problem is I can't work with cookies because they can be deleted client-side and I can't use the IP because they could just issue a new DHCP lease renewal.

So I would like to fallback to the MAC address of the clients. I'm aware that there is no java built in feature to get the MAC address. Is there a library that can handle the output of every OS? (primary Windows and Mac) since my java Application runs on both platforms.

or are there any other suggestions for uniquely identifying a client within a website and the HTTP Protocol ? (maybe HTML5 data stores or something else)

I'm using Java 1.7 btw.

I won't force the user to login or otherwise identify himself and I won't program a native app for the clients smartphone.

like image 395
Nexus2k Avatar asked Feb 12 '13 10:02

Nexus2k


2 Answers

I wrote my own method to solve my issue. Here it is if ever someone needs code to find a MAC address in the same network. Works for me without any admin privileges on 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 "";
}
like image 63
Nexus2k Avatar answered Nov 18 '22 11:11

Nexus2k


The best I could find is this: Query ARP cache to get MAC ID

And the potted summary is that:

  • there is no standard Java API,
  • there is no operating system independent solution,
  • your application typically needs to be privileged (e.g. root access) to query the host's ARP cache, and
  • if the packets go through a network router, you won't be able to identify the source MAC address anymore.

I don't think this is a good approach for identifying your user's machine.

Consider also that:

  • This only identifies the machine, not the user. Some computers are shared by multiple users.
  • MAC addresses can be changed too.
like image 37
Stephen C Avatar answered Nov 18 '22 11:11

Stephen C