Tracert or Traceroute in Java, without calling the OS

Asked

Viewed 611 times

3

I am developing a web application that will test the servers and will bring the result equal to a tracert of Windows or the traceroute Linux. I am developing in Java and using the commands mentioned in the following way:

I identify the server and call:

Runtime.getRuntime().exec("tracert " + hostCanonicalName);

After the execution I pick up the String and assemble a chart (Vis.js) with the path through which the request went.

I’ve already researched the web and I haven’t found anything in Java that does otherwise.

Is there any framework do that? I read something about HttpCliente and I did not receive a satisfactory reply.

I don’t want to use something directly related to the OS and I still can’t fully understand the commands, because with each IP I test it returns a different pattern.

1 answer

4


I also searched and on the first page appeared interesting results. I do not know if they solve what you want but seem to be what you need:

Code:

import java.net.InetAddress;

public class Ping {
    public static void main(String[] args) {
        try {
            if (args.length != 1) {
                System.out.println("Usage: java Ping <hostname>");
                System.exit(-1);
            }
            String host = args[0];
            int timeout = 3000;
            boolean status = InetAddress.getByName(host).isReachable(timeout);
            System.out.println(host + ": reachable? " + status);
        } catch (java.net.UnknownHostException e) {
            e.printStackTrace();
        } catch (java.io.IOException ioe) 
            ioe.printStackTrace();
        }
    }
}

I put in the Github for future reference.

The ping is the basis of traceroute. From it it is possible to construct an algorithm to plot the route.

  • The first link (Pingdom) forgets, the author has discontinued the project. Looking at the latest github tag, the source code is just a bean with 3 getters. Also, apparently this class is not used.

  • The design of the second link uses what’s in the third. I think it’s the way in case you don’t want to use Gordon Christie’s code for some reason.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.