From 7cf692d439c33e96a03df86a2ceb63512bf2eeb5 Mon Sep 17 00:00:00 2001 From: chenlw <874313221@qq.com> Date: Thu, 13 Oct 2016 14:19:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BF=90=E8=A1=8Cshell?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E7=9A=84=E6=96=B9=E6=B3=95=EF=BC=8C=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B8=A6=E7=AE=A1=E9=81=93=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=97=B6=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/platform/utils/ProcessMyUtil.java | 56 ++++++++--------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/src/com/platform/utils/ProcessMyUtil.java b/src/com/platform/utils/ProcessMyUtil.java index 74628834..725aa315 100644 --- a/src/com/platform/utils/ProcessMyUtil.java +++ b/src/com/platform/utils/ProcessMyUtil.java @@ -14,48 +14,30 @@ public class ProcessMyUtil { public List execCmdWaitAcquiescent(String cmd) { -// String host=Constant.hostIp; -// String username=Constant.rootUser; -// String password=Constant.rootPasswd; -// int port=Constant.port; - List reStrings = new ArrayList(); - + return runcmd(cmd); + } + + private List runcmd(String cmd) { + List result = new ArrayList(); + InputStream in = null; + String[] cmds = {"/bin/sh", "-c", cmd}; try { - ProcessBuilder pb = null; - Process pro = null; - - if (cmd.contains("grep")) { - List cmds = new ArrayList(); - 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; } }