Skip to content

Instantly share code, notes, and snippets.

@xola139
Created August 4, 2014 16:48
Show Gist options
  • Save xola139/fb50ce65984529c39a2f to your computer and use it in GitHub Desktop.
Save xola139/fb50ce65984529c39a2f to your computer and use it in GitHub Desktop.
Simple exec comand para linux, Este función hace la ejecución de un comando linux regresando la respuesta colocándola en un List
String cmd = request.getParameter("cmd");
//System.out.println(cmd);
List<String> lst=new ArrayList();
Runtime run = Runtime.getRuntime();
Process p = null;
//String cmd = "pwd";
try {
p = run.exec(cmd);
String s;
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((s = stdInput.readLine()) != null) {
//System.out.println(s);
lst.add(s);
}
p.getErrorStream();
p.waitFor();
}
catch (IOException e) {
e.printStackTrace();
//System.out.println("ERROR.RUNNING.CMD");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
p.destroy();
}
return lst;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment