packagelct.conference.test;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;public classTest3 {public static voidmain(String[] args){
String[] cmdStr= { "cmd", "/C", "wmic /node:\"192.168.0.34\" /user:\"administrator\" /password:\"12345\" service get /value"};
List> list = null;try{
list= getAllResult(cmdStr, 25);
System.out.println(list.size());
JSONArray jr=JSONArray.fromObject(list);
JSONObject jo= newJSONObject();for(int i=0;i
jo.clear();
jo=(JSONObject) jr.get(i);if("Apache Tomcat".equals(jo.get("Caption"))){
String ifStarted= jo.get("Started").toString();
System.out.println("Apache Tomcat服务"+ifStarted);
}else if("MySQL".equals(jo.get("Caption"))){
String ifStarted= jo.get("Started").toString();
System.out.println("MySQL服务"+ifStarted);
}else if("PCMS Service".equals(jo.get("Caption"))){
String ifStarted= jo.get("Started").toString();
System.out.println("PCMS Service服务"+ifStarted);
}else if("PCMS Watch Service".equals(jo.get("Caption"))){
String ifStarted= jo.get("Started").toString();
System.out.println("PCMS Watch Service服务"+ifStarted);
}
}
System.out.println(list);
}catch(IOException e) {
e.printStackTrace();
System.out.println("获取所有服务信息失败!");
}
}private static List> getAllResult(String[] cmdStr, int flag) throwsIOException {
List> list = new ArrayList>();
Integer index= 1;
Process p= null;
String str= null;
String[] arrStr= new String[2];
Map map = new HashMap();
InputStreamReader isr= null;
BufferedReader br= null;try{
p=Runtime.getRuntime().exec(cmdStr);
isr= new InputStreamReader(p.getInputStream(),"GBK");
br= newBufferedReader(isr);while ((str = br.readLine()) != null) {if (str!=null && !"".equals(str)) {if (index % flag == 0) {
list.add(map);
map= new HashMap();
}
arrStr= str.split("=");
str= str.endsWith("=") ? "" : arrStr[1];
map.put(arrStr[0], str);
index++;
}
}
}catch(IOException e) {
e.printStackTrace();
System.out.println("获取进程的所有信息失败!");throwe;
}catch(Exception e) {
e.printStackTrace();
System.out.println("获取执行结果失败!");
}finally{try{if (br != null) {
}
br.close();if (isr != null) {
isr.close();
}
}catch(IOException e) {
e.printStackTrace();
}if (p != null) {
p.destroy();
}
}returnlist;
}
}