博主,你好!有个问题请教一下。我从android app上执行route add指令时提示“route: SIOCADDRT: Operation not permitted”这个错误,app已获取root权限,且该命令通过adb shell可以执行成功,但是在这个APP 里执行这个shell命令就报这个错误,app执行“busybox route -n”可以成功。请问这是为什么呢?求指教!具体代码如下:
public static void addRoute(String dst,String mask,String nextjump){
String dstIP = dst.substring(0,dst.lastIndexOf("."));
String command = "su -c busybox route add -net "+dstIP+".0 netmask "+mask+" gw "+nextjump+" dev eth0";
CommandResult rs = shellCommand(command, true);
}
public static CommandResult shellCommand(String[] commands, boolean isRoot) {
CommandResult commandResult = new CommandResult();
if (commands == null || commands.length == 0) return commandResult;
Process process = null;
DataOutputStream os = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command != null) {
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
}
os.writeBytes(COMMAND_EXIT);
os.flush();
commandResult.result = process.waitFor();
//获取错误信息
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) successMsg.append(s);
while ((s = errorResult.readLine()) != null) errorMsg.append(s);
commandResult.successMsg = successMsg.toString();
commandResult.errorMsg = errorMsg.toString();
Log.i(TAG, commandResult.result + " | " + commandResult.successMsg
+ " | " + commandResult.errorMsg);
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e(TAG, errmsg);
} else {
e.printStackTrace();
}
} catch (Exception e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e(TAG, errmsg);
} else {
e.printStackTrace();
}
} finally {
try {
if (os != null) os.close();
if (successResult != null) successResult.close();
if (errorResult != null) errorResult.close();
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e(TAG, errmsg);
} else {
e.printStackTrace();
}
}
if (process != null) process.destroy();
}
return commandResult;
}