Java create wifi hotspot in Windows 8

2019-05-18 02:36发布

问题:

I need to create a wifi hotspot and a DHCP server in Windows 7-8 with Java. I'm developing a software that could be for the user less intrusive and simple that i could.

For the first part of my work, I thought to use netsh to create the hosted network and to set static the ip.

my current code is this:

String[] command = {  "cmd", };
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());

//now I use the stdin.println for my shell commands

stdin.println("netsh wlan set hostednetwork mode=allow ssid=mynetwork key=mypassword");
stdin.println("netsh wlan start hostednetwork");
stdin.println("netsh interface ipv4 set address \"Wi-Fi\" static 192.168.1.2 255.255.255.0 192.168.1.254");

Problems:

1) I need to elevate the privileges to administrator. I tried to use "elevate.exe" http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/ (found in an other stackoverflow question) it work's well, but if I have three calls he ask me three times to execute the command with admin privileges..and this isn't very user friendly.

I tried also to use "runas":

runas /noprofile /user:administrator netsh ......

But in this case the problem is that: the administrator user have to be active otherwise I have to found a way to scan all the active users with administration permissions. In addiction after the runas I must interact with prompt and write the password.

2) There is a way to scan all the wifi interfaces for the last netsh command?

回答1:

I was researching a little bit about elevate tool and I made some tests. Here's the key to make it work (from the documentation):

Usage: Elevate [-?|-wait|-k] prog [args]

-k    - Starts the the %comspec% environment variable value and
        executes prog in it (CMD.EXE, 4NT.EXE, etc.)

prog  - The program to execute

I downloaded the elevate.exe executable and put it in my NetBeans project folder. Then I create a single process for each netsh command you want to run (I made a little modifications but only for testing purposes).

Disclaimer: Tested on Windows 7 Home, not Windows 8.

Finally, as @damryfbfnetsi pointed out in his comment, you should use ProcessBuilder instead of Runtime as follows:

public static void main(String[] args) {        
    try {

        System.out.println("-- Setting up WLAN --");
        String netshCommand = "netsh wlan set hostednetwork mode=allow ssid=\"YourSSID\" key=\"YourPassword\" & exit";
        String[] elevateCommand = new String[]{"./Release/elevate.exe", "-wait", "-k", "prog", netshCommand};
        ProcessBuilder pb1 = new ProcessBuilder(elevateCommand);
        Process p1 = pb1.start();
        p1.waitFor();

        System.out.println("-- Starting WLAN --");
        netshCommand = "netsh wlan start hostednetwork & exit";
        elevateCommand = new String[]{"./Release/elevate.exe", "-wait", "-k", "prog", netshCommand};
        ProcessBuilder pb2 = new ProcessBuilder(elevateCommand);
        Process p2 = pb2.start();
        p2.waitFor();

        System.out.println("-- Setting up IPv4 interface --");
        netshCommand = "netsh interface ipv4 set address \"Conexión de red inalámbrica\" static 192.168.0.102 255.255.255.0 192.168.0.254 & exit";
        elevateCommand = new String[]{"./Release/elevate.exe", "-wait", "-k", "prog", netshCommand};
        ProcessBuilder pb3 = new ProcessBuilder(elevateCommand);
        Process p3 = pb3.start();
        p3.waitFor();

        System.out.println("-- Getting IPv4 interface dump --");
        netshCommand = "netsh interface ipv4 dump";
        ProcessBuilder pb4 = new ProcessBuilder("cmd.exe", "/c", netshCommand);
        Process p4 = pb4.start();

        System.out.println("-- Printing IPv4 interface dump --");
        BufferedReader bfr = new BufferedReader(new InputStreamReader(p4.getInputStream(),"ISO-8859-1"));
        String output;
        while((output = bfr.readLine()) != null){
            System.out.println(output);
        }

    } catch (IOException | InterruptedException ex) {
        ex.printStackTrace();
    }

}

Note: please note netshCommand ends with & exit. That's because if it doesn't then the cmd console open by elevate.exe will remain open.

The output should look like this (sorry for the Spanish words but I have my Windows in that language):

-- Setting up WLAN --
-- Starting WLAN --
-- Setting up IPv4 interface --
-- Getting IPv4 interface dump --
-- Printing IPv4 interface dump --

#----------------------------------
# Configuración de IPv4
#----------------------------------
pushd interface ipv4

reset set global icmpredirects=enabled
add route prefix=0.0.0.0/0
interface="Conexión de red inalámbrica" nexthop=192.168.0.254 metric=1 publish=Sí 
add address name="Conexión de red inalámbrica" address=192.168.0.102 mask=255.255.255.0

popd
# Fin de la configuración de IPv4