package cn.ty.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SysUtils {
/**
* FunName: executeSys
* Description : 执行系统命令并返回一段字符串
* @param: 系统命令:String
* @return String: 返回执行的结果集
* @Create Date: 2010-03-25
*/
public static byte[] executeSy(String command) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec(command);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
try {
while(null!=(inline=br.readLine())){
sb.append(inline).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
String reuslts = sb.toString();
byte[] bytes = reuslts.getBytes();
return bytes;
}
/**
* FunName: executeSysString
* Description : 执行系统命令并返回一段字符串
* @param: 系统命令:String
* @return String: 返回执行的结果集
* @Create Date: 2010-03-25
*/
public static String executeSysString(String command) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec(command);
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
try {
//注意:一般如果我们调用ifconfig命令的话是会有空行的。这里我们通过
//inline.length()的技巧解决了这个问题。成功将空行去掉。记号一下!
while(null!=(inline=br.readLine())){
if(inline.length() > 0) {
sb.append(inline).append("\n");
continue;
} else {
continue;
}
}
} catch (IOException e) {
e.printStackTrace();
}
String reuslts = sb.toString();
return reuslts;
}
/**
* FunName: executeSys
* Description : 执行系统命令
* @param: 系统命令:String
* @Create Date: 2010-03-25
*/
public static void executeCommand(String command) {
System.out.println(command);
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String out = SysUtils.executeSysString("ipconfig");
System.out.println(out);
//SysUtils.executeCommand("kill -9 3104");
}
}
阅读(1351) | 评论(0) | 转发(0) |