Implement Traceroute functionality in android

天大地大妈咪最大 提交于 2019-12-07 06:09:07

问题


This is the first time I am asking any question so forgive me for my any mistake.

I want to implement traceroute functionality like this apps available in android play store.

Visual TracertPro

Traceroute

I know that when typing in CMD in windows traceroute google.com will display all intermediate IP used.

Now what I have tried.

I try using traceroute command but android not support traceroute only rooted device support it.

Process process =Runtime.getRuntime().exec("traceroute yahoo.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int i;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((i = reader.read(buffer)) > 0)
    output.append(buffer, 0, i);
reader.close();
Log.d("*************", ""+output);

So I thought to achieve using ping command but couldn't get any success.By using ping command it only gives ip of google.com not as I need and above apps display.

Process process = Runtime.getRuntime().exec("/system/bin/ping -t 1 -c 1 google.com");

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int i;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((i = reader.read(buffer)) > 0)
    output.append(buffer, 0, i);
reader.close();
Log.d("*************", ""+output);

Please guide me how to achieve this. Some link or some tutorial will be very useful.

Thanking you in advance.


回答1:


The busybox utility includes traceroute. You can run busybox on your device without rooting your phone by following this youtube tutorial. You should then be able to use the first code segment you posted to query traceroute from within your app. Of course, you will need to make sure that you use the correct path when calling traceroute.




回答2:


Traceroute works by sending 3 ICMP messages via UDP to an unlikely port number, starts a timer each time, and setting the TTL in the datagram to 1 (max 1 hop). When it reaches the first router, the datagram expires and the router will refuse to send the packet any farther and respond with a expired message (ICMP type 11 code 0) that includes the routers name, IP address, MTU, and a few other bits of data. The source will obtain the round trip time from the timer. Once traceroute gets the data from the first hop, it will repeat these steps but increment the TTL by one, and keep repeating these steps until it finally gets to the host itself.

In order for your code to work, you need to be able to manipulate the network layer datagram and set the TTL value in the header. After that it's just a matter of parsing out the responses from each router that returns a TTL expired message.



来源:https://stackoverflow.com/questions/15804499/implement-traceroute-functionality-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!