我试图在Android上使用/system/bin/ping命令来ping我的网络设备。但无论我尝试ping的目标是www.google.com、192.168.2.1(路由器本地IP)或子网中其他任何可能的IP地址,它都返回退出码2。以下是我的代码。
onCreate
我尝试过使用超时和不使用超时,但是唯一打印的退出代码是2。有什么想法吗?
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String subnet = "192.168.2";
for (int i = 0; i <= 255; i++) {
String node = subnet + "." + i;
System.out.println(pingHost(node));
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
pingHost()
public static int pingHost(String host) throws IOException, InterruptedException {
String cmd = "/system/bin/ping -c 1 -W 1000 " + host;
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
return proc.exitValue();
}
我尝试过使用超时和不使用超时,但是唯一打印的退出代码是2。有什么想法吗?