📄 processcommand.java
字号:
package com.sxit.wap.sncs;
/**
* <p>Title: wap II</p>
* <p>Description: 处理请求cpu,disk等信息的命令处理类</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: sxit</p>
* @author not attributable
* @version 1.0
*/
/*
运行于被监控的机器上
*/
import java.io.*;
import java.util.*;
public class ProcessCommand {
public ProcessCommand() {
}
private String getCommandExec(String cmd){
String list = "";
//cmd = "ping localhost";
try {
//根据系统不同,动态的调用命令cmd 或者bash
Process proc = Runtime.getRuntime ().exec ( cmd );
InputStream istr = proc.getInputStream ();
BufferedReader br =
new BufferedReader ( new InputStreamReader ( istr ) );
String str = "";
while ( ( str = br.readLine () ) != null )
list= list + str ;
} catch (Exception e1) {
e1.printStackTrace();
}
return list.trim();
}
public String getCpuInfo() {
return getCommandExec("top");
}
public String getDiskInfo() {
return getCommandExec("disk");
}
public boolean isPingOk(String ip){
String list = "" ;
int times = 2;
while (times-- >0) {
try {
list = getCommandExec( "ping " + ip.trim () );
System.out.println(list) ;
int position = list.indexOf ( "Lost = " );
System.out.println(position) ;
if ( position > 0 ) {
int position2 = list.indexOf ( "%", position );
System.out.println(position2) ;
position = list.indexOf ( "(", position );
System.out.println(position) ;
int lostRate = Integer.parseInt ( list.substring ( position+1, position2 ) );
System.out.println(lostRate) ;
if ( lostRate == 0 )
break;
else
if ( times == 0 )
return false;
} else
return false;
} catch ( Exception e1 ) {
e1.printStackTrace ();
}
}
return true;
}
public static void main(String[] args) {
ProcessCommand processCommand = new ProcessCommand();
//System.out.println( processCommand.isPingOk("192.168.8.30") ? "OK":"failed");
System.out.println( processCommand.getCommandExec("192.168.8.30") );
//System.out.println( processCommand.isPingOk("192.168.8.3") ? "OK":"failed");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -