How do I get the hosts mac address using Java 5?

2020-01-29 16:56发布

问题:

I know you can do this with Java 6 using java.net.NetworkInterface->getHardwareAddress(). But the environment I am deploying on is restricted to Java 5.

Does anyone know how to do this in Java 5 or earlier? Many thanks.

回答1:

The standard way in Java 5 was to start a native process to run ipconfig or ifconfig and parse the OutputStream to get your answer.

For example:

private String getMacAddress() throws IOException {
    String command = “ipconfig /all”;
    Process pid = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
    Pattern p = Pattern.compile(”.*Physical Address.*: (.*)”);
    while (true) {
        String line = in.readLine();
        if (line == null)
            break;
        Matcher m = p.matcher(line);
        if (m.matches()) {
            return m.group(1);
        }
    }
}


回答2:

Butterchicken's solution is ok, but will only work on english versions of Windows.

A somewhat better (language independent) solution would be to match the pattern for MAC addresses. Here I also make sure that this address has an associated IP (e.g. to filter out bluetooth devices):

public String obtainMacAddress()
throws Exception {
    Process aProc = Runtime.getRuntime().exec("ipconfig /all");
    InputStream procOut = new DataInputStream(aProc.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(procOut));

    String aMacAddress = "((\\p{XDigit}\\p{XDigit}-){5}\\p{XDigit}\\p{XDigit})";
    Pattern aPatternMac = Pattern.compile(aMacAddress);
    String aIpAddress = ".*IP.*: (([0-9]*\\.){3}[0-9]).*$";
    Pattern aPatternIp = Pattern.compile(aIpAddress);
    String aNewAdaptor = "[A-Z].*$";
    Pattern aPatternNewAdaptor = Pattern.compile(aNewAdaptor);

    // locate first MAC address that has IP address
    boolean zFoundMac = false;
    boolean zFoundIp = false;
    String foundMac = null;
    String theGoodMac = null;

    String strLine;
    while (((strLine = br.readLine()) != null) && !(zFoundIp && zFoundMac)) {
        Matcher aMatcherNewAdaptor = aPatternNewAdaptor.matcher(strLine);
        if (aMatcherNewAdaptor.matches()) {
            zFoundMac = zFoundIp = false;
        }
        Matcher aMatcherMac = aPatternMac.matcher(strLine);
        if (aMatcherMac.find()) {
            foundMac = aMatcherMac.group(0);
            zFoundMac = true;
        }
        Matcher aMatcherIp = aPatternIp.matcher(strLine);
        if (aMatcherIp.matches()) {
            zFoundIp = true;
            if(zFoundMac && (theGoodMac == null)) theGoodMac = foundMac;
        }
    }

    aProc.destroy();
    aProc.waitFor();

    return theGoodMac;
}


回答3:

As far as I know there is no pure pre Java 6 solution. UUID solves this but first determine OS to find out if it should run ifconfig or ipconfig.



回答4:

On Linux and Mac OS X Machine , you might have to use ifconfig -a.
ipconfig is as windows command.



标签: java java-5