我正在使用psexec
并sc query state= all
打印出远程服务器上的所有服务。我想解析这个的输出,并一直在尝试使用 aBufferedReader
来做到这一点。
Runtime rt = Runtime.getRuntime();
String line = null;
Process pr = null;
pr = rt.exec("test.bat");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
while((line = input.readLine()) != null) {
System.out.println(line);
}
测试.bat
psexec \\server -u username -p password sc query state= all
当我psexec \\server -u username -p password sc query state= all
从命令行窗口运行时,我得到了这个;
SERVICE_NAME: Tomcat6
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
SERVICE_NAME: ActiveMQ
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
etc.
但是在Java中,它打印第一个服务然后停止,所以打印出来的是这样的;
SERVICE_NAME: Tomcat6
TYPE : 10 WIN32_OWN_PROCESS
STATE : 4 RUNNING
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0