For everyone having the same problem.... I used as a code the example from Run shell commands from android program The use of threads and sleep time solved the problem for me
public void runAsRoot(String[] cmds) throws Exception {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
InputStream is = p.getInputStream();
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
int readed = 0;
byte[] buff = new byte[4096];
boolean cmdRequiresAnOutput = true;
if (cmdRequiresAnOutput) {
while( is.available() <= 0) {
try { Thread.sleep(5000); } catch(Exception ex) {}
}
while( is.available() > 0) {
readed = is.read(buff);
if ( readed <= 0 ) break;
String seg = new String(buff,0,readed);
result=seg; //result is a string to show in textview
}
}
}
os.writeBytes("exit\n");
os.flush();
the important part is the Thread.sleep(5000);
For "ls" the sleep time is not important. But for commands like ping or traceroute you need time and you have to wait for the results, hence 5000ms=5seconds is enough to get a response for a ping HOP.