Get MAC address of docking station ignoring MAC address pass through

743 views Asked by At

I have the goal to identify docking stations by their MAC address for an office application to automate which desks are occupied. With different docking stations it works fine. However, I cannot achieve this when a Dell Laptop is connected to a Dell docking station because they use MAC address pass through. Thus, they use a MAC address of the laptop, and I cannot request the MAC address of the docking station.

Has anyone an idea how I can get this MAC address with Java or maybe with which command I can achieve this? I have not found anything because all approaches just give me the MAC address of the laptop. The solution does not have to be platform independent.

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MacAddressReader {
    public static String getMacAddressOfDockingStation(String interfaceName) {
        String macAddress = getAllInterfacesNamesAndMacs().get(interfaceName);
        if (macAddress != null && !macAddress.isEmpty())
            return macAddress;

        return "";
    }

    private static Map<String, String> getAllInterfacesNamesAndMacs() {
        Map<String, String> addresses = new HashMap<>();
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                addresses.put(
                        networkInterface.getDisplayName(),
                        macAddressAsString(networkInterface.getHardwareAddress())
                );
            }
            return addresses;
         } catch (SocketException e) {
            return addresses;
         }
    }

    private static String macAddressAsString(byte[] mac) {
        if (mac == null)
            return "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
        }
        return sb.toString();
    }

}
1

There are 1 answers

0
Thorsten On

I was not able to solve the problem using the MAC address. If anyone has the same problem you could maybe use the connected monitors to uniquely identify the workstation. With a simple powershell skript you can get a unique serial number for a monitor to identify a workstation. This is my code, maybe this can help someone.

Powershell skript:

$Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi

function Decode {
    If ($args[0] -is [System.Array]) {
        [System.Text.Encoding]::ASCII.GetString($args[0])
    }
    Else {
        "Not Found"
    }
}

ForEach ($Monitor in $Monitors) {
    $Manufacturer = Decode $Monitor.ManufacturerName -notmatch 0
    $Product = Decode $Monitor.ProductCodeID -notmatch 0
    $Name = Decode $Monitor.UserFriendlyName -notmatch 0
    $Serial = Decode $Monitor.SerialNumberID -notmatch 0

    echo "$Manufacturer;$Product;$Name;$Serial"
}

Java code with jPowerShell:

try (PowerShell powerShell = PowerShell.openSession()){    
     String script = "../MyMonitorScript.txt";
     String result = powerShell.executeScript(script).getCommandOutput();
     ...

The String result contains information about the connected monitors including the unique serial number.