java获取远程服务器应用程序服务状态
package lct.conference.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.sf.json.JSONArray;import net.sf.json.JSONObject;public class Test3 { public static void main(String[] args){ String[] cmdStr = { "cmd", "/C", "wmic /node:\"192.168.0.34\" /user:\"administrator\" /password:\"12345\" service get /value" }; List<Map<String, Object>> list = null; try { list = getAllResult(cmdStr, 25); System.out.println(list.size()); JSONArray jr = JSONArray.fromObject(list); JSONObject jo = new JSONObject(); for(int i=0;i<list.size();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<Map<String, Object>> getAllResult(String[] cmdStr, int flag) throws IOException { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Integer index = 1; Process p = null; String str = null; String[] arrStr = new String[2]; Map<String, Object> map = new HashMap<String, Object>(); InputStreamReader isr = null; BufferedReader br = null; try { p = Runtime.getRuntime().exec(cmdStr); isr = new InputStreamReader(p.getInputStream(),"GBK"); br = new BufferedReader(isr); while ((str = br.readLine()) != null) { if (str!=null && !"".equals(str)) { if (index % flag == 0) { list.add(map); map = new HashMap<String, Object>(); } arrStr = str.split("="); str = str.endsWith("=") ? "" : arrStr[1]; map.put(arrStr[0], str); index++; } } } catch (IOException e) { e.printStackTrace(); System.out.println("获取进程的所有信息失败!"); throw e; } 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(); } } return list; }}
赞 (0)