The code below displays the subnet mask. On a computer with more than one network connection (for example, a laptop with a wireless connection and a Cat-5 Ethernet connection), it can write the subnet mask twice, because there can be two different IP addresses for the client.
String os = System.getProperty("os.name"); try { if(os.indexOf("Windows 7")>=0) { Process process = Runtime.getRuntime().exec("ipconfig"); process.waitFor(); InputStream commandOut= process.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(commandOut)); String line; while((line = in.readLine()) !=null) { if(line.indexOf("Subnet Mask")>=0) { int colon = line.indexOf(":"); System.out.println(line.substring(colon+2)); } } } catch(IOException ioe) { } catch(java.lang.InterruptedException utoh) { }
On my laptop with an active wired and wireless connection, I get this output: 255.255.254.0 255.255.254.0
When I turn off my wireless connection, I see only one line of output for the wired Ethernet line, as expected.
source share