我正在研究一些关于提升工具的信息,并进行了一些测试。这是使其工作的关键(来自文档):
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
我下载了elevate.exe
可执行文件并将其放在我的 NetBeans 项目文件夹中。netsh
然后,我为您要运行的每个命令创建一个进程(我做了一些修改,但仅用于测试目的)。
免责声明:在 Windows 7 家庭版而非 Windows 8 上测试。
最后,正如@damryfbfnetsi在他的评论中指出的那样,您应该使用ProcessBuilder
而不是Runtime
如下:
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();
}
}
注意:请注意以 .netshCommand
结尾& exit
。那是因为如果没有,那么cmd
打开的控制台elevate.exe
将保持打开状态。
输出应该是这样的(对不起西班牙语单词,但我的 Windows 是那种语言的):
-- 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