Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Butterchicken's solution is ok, but will only work on english versions of Windows.</p> <p>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):</p> <pre><code>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) &amp;&amp; !(zFoundIp &amp;&amp; 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 &amp;&amp; (theGoodMac == null)) theGoodMac = foundMac; } } aProc.destroy(); aProc.waitFor(); return theGoodMac; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload