//执行Linux命令的方法,参数为命令语句,如 route -n
public String runShell(String shStr) throws Exception {
Process process;
process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", shStr});
process.waitFor();
new Thread(){
@Override
public void run() {
System.out.println(process.getErrorStream());
}
}.start();
BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
String result = "";
while ((line = read.readLine()) != null) {
result += line;
result += " ";
}
return result;
}
public String getRoute(){
String str = null;
try {
str=runShell("route -n");//调用函数执行 route -n 命令查询路由信息
} catch (Exception e) {
return e.getMessage();
}
return str;
}