Java crea hotspot wifi in Windows 8


Ho bisogno di creare un hotspot wifi e un server DHCP in Windows 7-8 con Java. Sto sviluppando un software che potrebbe essere per l'utente meno invadente e semplice che potrei.

Per la prima parte del mio lavoro, ho pensato di usare netsh per creare la rete ospitata e impostare staticamente l'ip.

Il mio codice attuale è questo:

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");

Problemi:

1) Ho bisogno di elevare i privilegi all'amministratore. Ho provato ad usare " elevare.exe" http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line / (trovato in un'altra domanda stackoverflow) funziona bene, ma se ho tre chiamate mi chiede tre volte di eseguire il comando con privilegi di amministratore..e questo non è molto facile da usare.

Ho provato anche ad usare"runas":

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

Ma in questo caso il problema è che: l'utente amministratore deve essere attivo altrimenti devo trovare un modo per scansionare tutti gli utenti attivi con l'amministrazione permesso. In aggiunta dopo le runas devo interagire con il prompt e scrivere la password.

2) C'è un modo per scansionare tutte le interfacce wifi per l'ultimo comando netsh?

Author: Danilo, 2013-08-21

1 answers

Stavo cercando un po ' su elevare strumento e ho fatto alcuni test. Ecco la chiave per farlo funzionare (dalla documentazione):

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

Ho scaricato l'eseguibile elevate.exe e l'ho inserito nella cartella del progetto NetBeans. Quindi creo un singolo processo per ogni comando netsh che vuoi eseguire (ho apportato alcune modifiche ma solo a scopo di test).

Disclaimer : Testato su Windows 7 Home, non su Windows 8.

Infine, come @ damryfbfnetsi ha sottolineato nel suo commento, dovresti usare ProcessBuilder invece di Runtime come segue:

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();
    }

}

Nota: si prega di notare netshCommand termina con & exit. Questo perché se non lo fa, la console cmd aperta da elevate.exe rimarrà aperta.

L'output dovrebbe essere simile a questo (mi dispiace per le parole spagnole ma ho le mie Finestre in quella lingua):

-- 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
 0
Author: dic19, 2017-05-23 12:28:15