Search code examples
javawindowsnetwork-programmingwifinetsh

Accessing Wi-Fi network interface details from Java using netsh


I am developing a java program on windows platform. I want to access Wi-Fi interface details such as SSID, ip, subnet, enabled or not, connected or not and so on. Currently i am trying with using netsh, ipconfig commands. For example here is how i get the connected SSID. Is this way good ? Is there any better easier method ? Thanks in advance.

Will this code work all all windows versions without any problem ?

String ssid;
ProcessBuilder builder = new ProcessBuilder(
        "cmd.exe", "/c", "netsh wlan show interfaces");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
    line = r.readLine();
    if (line.contains("SSID")){
        ssid = line.split("\\s+")[3];
        System.out.println(ssid);
        return ssid;
    }
}

Solution

  • There are differences in the NetSh.exe App/Command from Windows OS to Windows OS.

    Microsoft Command-Line Reference on NetSh states "There are functional differences between netsh commands on Windows Server® 2003, Windows Server® 2008, and Windows Server® 2008 R2:"

    Meaning the there are differences in XP, Vista, and Windows 7. However, just because there are changes doesn't mean you can't try testing the few commands you want to use in your App. I say this because NetSh.exe has a really large set of capabilites with lots of subcommands spanning across many networking technologies.

    In order to test the differences from OS to OS you could use a script the lists the NetSh.exe help menu's on multiple OSs or compare the example commands on Microsofts website (at below link).

    RobVanderwoude has a script the shows all the subcommands for NetSh. If you took the script a step further you could list out all the subcommand examples and compare the result between OS.

    Still, you have to test the command manually on each OS or confirm from people you trust that a certain command worked on an OS.

    NetSh.exe CLI Ref: https://technet.microsoft.com/en-us/library/ee404790.aspx

    NetSh Help Script: http://www.robvanderwoude.com/netsh.php