Java没有用于ICMP和traceroute的原语。如何克服这一点?基本上,我正在构建应该在*nix和Windows中运行的代码,并且需要一段可以在这两个平台上运行的代码。
发布于 2013-01-23 07:16:41
以下是我今天编写的用Java“实现”trace route命令的代码。我只在windows上测试过,但它在Linux上应该也能工作,尽管有几个适用于Linux的traceroute工具,所以很可能需要一些检查这些程序的存在。
public class NetworkDiagnostics{
private final String os = System.getProperty("os.name").toLowerCase();
public String traceRoute(InetAddress address){
String route = "";
try {
Process traceRt;
if(os.contains("win")) traceRt = Runtime.getRuntime().exec("tracert " + address.getHostAddress());
else traceRt = Runtime.getRuntime().exec("traceroute " + address.getHostAddress());
// read the output from the command
route = convertStreamToString(traceRt.getInputStream());
// read any errors from the attempted command
String errors = convertStreamToString(traceRt.getErrorStream());
if(errors != "") LOGGER.error(errors);
}
catch (IOException e) {
LOGGER.error("error while performing trace route command", e);
}
return route;
}发布于 2016-04-19 03:17:05
您将需要jpcap library (也许SourceForge jpcap也可以工作),并使用ICMPPacket类来实现所需的功能。
https://stackoverflow.com/questions/2627706
复制相似问题