'netstat' 命令在 Android 上是否普遍可用?

Is the 'netstat' command generically available on Android?

我目前使用以下代码来获取有关活动网络套接字的一些信息。到目前为止,我已经确认这适用于 Nexus 4 (Jellybeans)、Nexus 5 (Jellybeans、KitKat 和 Lollipop) 和 Sony Xperia 设备 (Jellybeans)。

Process process = Runtime.getRuntime().exec("netstat -n");
process.getOutputStream().close();
BufferedReader reader = new BufferedReader(
        new InputStreamReader(process.getInputStream()));

String line;
while ((line = reader.readline()) != null) {
    // Parse line for required info
}

reader.close();

我能否依靠上述代码在大多数现实世界 Android 设备上正常运行?

Android shell 命令大部分位于设备上的 /system/bin 文件夹中。 Vanilla Android 使用包含 netstat 的 FreeBSD 工具箱,但我不确定您是否可以 100% 确定它会一直存在,因为 OEM 有时会附带不同的工具。这是工具箱中包含的工具的示意图:

您可以查看 source for vanilla Android 以查看哪些版本附带了哪些内容。但正如我所说,每个制造商随每台设备提供的产品可能有所不同。