7

Java には、ICMP と traceroute のプリミティブがありません。これを克服する方法は?基本的に、私は *nix と Windows で実行するコードを作成しており、両方のプラットフォームで実行できるコードが必要です。

4

2 に答える 2

5

以下は、Java でトレース ルート コマンドを「実装」するために今日書いたものです。私は 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;
}
于 2013-01-22T23:16:40.563 に答える