修改运行shell命令的方法,,修复带管道命令时的bug

web_gfs
chenlw 8 years ago
parent 2238e7fad0
commit 7cf692d439

@ -14,48 +14,30 @@ public class ProcessMyUtil {
public List<String> execCmdWaitAcquiescent(String cmd) {
// String host=Constant.hostIp;
// String username=Constant.rootUser;
// String password=Constant.rootPasswd;
// int port=Constant.port;
List<String> reStrings = new ArrayList<String>();
return runcmd(cmd);
}
private List<String> runcmd(String cmd) {
List<String> result = new ArrayList<String>();
InputStream in = null;
String[] cmds = {"/bin/sh", "-c", cmd};
try {
ProcessBuilder pb = null;
Process pro = null;
if (cmd.contains("grep")) {
List<String> cmds = new ArrayList<String>();
cmds.add("sh");
cmds.add("-c");
cmds.add(cmd);
new ProcessBuilder(cmds);
pro = pb.start();
}else {
pro = Runtime.getRuntime().exec(cmd);
}
if (null == pro) {
return reStrings;
}
BufferedReader br = new BufferedReader(new InputStreamReader(pro.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
if (line != null) {
// System.out.println(line);
reStrings.add(line);
} else {
break;
}
}
if(reStrings.size()==0){
Process pro = Runtime.getRuntime().exec(cmds);
pro.waitFor();
in = pro.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line=read.readLine()) != null) {
result.add(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return reStrings;
// String[] strs = new String[result.size()];
// return result.toArray(strs);
return result;
}
}

Loading…
Cancel
Save