java 读取远程服务器信息吗_java获取远程服务器应用程序服务状态

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值