0

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();
    }

}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Thorsten
  • 31
  • 4
  • You assume this docking station has its own MAC address, but is that really the case (otherwise: why would it pass through the laptops MAC address)? – Mark Rotteveel Nov 04 '21 at 16:46
  • It has it own MAC address according to this https://www.dell.com/support/kbdoc/en-us/000143263/what-is-mac-address-pass-through for laptops which do not support mac address pass through. – Thorsten Nov 04 '21 at 17:00

1 Answers1

0

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.

Thorsten
  • 31
  • 4