Detailed explanation of traceroute and tracert usage

1. What is Traceroute ?  
               The Internet, that is, the Internet, is the largest computer network in the world, or more precisely, a network of networks. It consists of tens of thousands of local area networks and millions of computers all over the world, and communicates between networks through the
TCP/IP protocol for heterogeneous networks. In the Internet, information is transmitted from one end to another through many segments of transmission media and devices (routers, switches, servers, gateways, etc.) in the network. Each device connected to the Internet, such as a host, router, access server, etc., generally has an independent IP address. Through Traceroute we can know what path the information takes from your computer to the host on the other end of the Internet. Of course, each time the data packet travels from the same source to the same destination, the path may be different, but basically the route taken is the same most of the time. In UNIX systems, we call it Traceroute, and in MS Windows it is Tracert. Traceroute measures how long it takes by sending small packets to the destination device until it returns. Each device on a path Traceroute needs to be tested 3 times. The output includes the time (ms) for each test and the name of the device (if any) and its IP address.
               In most cases, as a network engineer or system administrator, under the UNIX host system, directly execute the command line:

               Traceroute [parameter options] hostname     (hostname can be a domain name or an ip address)

Parameter options:
               -i specifies the network interface, useful for multiple network interfaces. For example -i eth1 or -i ppp1, etc.;
               -m sets the maximum lifetime used in outgoing probe test packets to max-ttl forwarding times, the default value is 30 times;
               -n Display IP address, do not check hostname. This parameter is often used when DNS does not work;
               -p port The basic UDP port used by the probe packet is set to port, the default value is 33434
               -qn Set the number of probe packets to the value n each time the lifetime is set, and the default is 3;
               -r bypasses the normal routing table and sends directly to the host connected to the network;
               -wn Set the waiting response time for outgoing probe packets to n seconds, the default value is 3 seconds;

tracert of windows

C:\>tracert

Usage: tracert [-d] [-h maximum_hops] [-j host-list] [-w timeout] target_name 

Parameter descriptions:
tracert [-d] [-h maximum_hops] [-j computer-list] [-w timeout] target_name
This diagnostic utility works by sending Internet Control Message Protocol (CMP) with different time-to-live (TL) to the destination Reply messages to determine the route to the destination. Each router on the path must reduce its TTL value by at least 1 before forwarding the ICMP response, so the TTL is a valid hop count. When the TTL value of the packet is reduced to 0, the router sends back ICMP timeout information to the source system. Tracert can determine the route by sending the first reply with a TTL of 1 and incrementing the TTL value by 1 each time in subsequent transmissions until the target responds or the maximum TTL value is reached. Routers can be identified by examining the ICMP timeout (ime Exceeded) messages sent back by intermediate routers. Note that some routers "quietly" discard packets with expired time-to-live (TLS) and have no effect on tracert.
Parameters:
    -d Specifies not to resolve addresses for computer names.
    -h maximum_hops specifies the maximum number of hops to find the target.
    -jcomputer-list Specifies loose source routing in computer-list.
    -w timeout Wait the number of milliseconds specified by timeout for each reply.
    target_name The name of the target computer, which can be a domain name or an IP address.

Suddenly I was thinking, this time the submarine cable is hung up, where is the backup route?
Try tracert and see which jumps have passed through 
C:\>tracert blog.is36.cn
 
Tracing route to blog.is36.cn [68.68.99.173]
over a maximum of 30 hops:
 
  1 3 ms 8 ms 10 ms 172.31.8.254 //Last hop on local LAN
  2 9 ms 9 ms 9 ms 172.8.105.254 //Continue to the previous hop
  3 3 ms 9 ms 9 ms 10.1.2.9 //Continue to the previous hop
  4 1 ms 4 ms 1 ms 10.1.2.2 //Exit (internal address)
  5 <1 ms <1 ms <1 ms 60.191.53.193 //Exit (external address)
  6 <1 ms <1 ms <1 ms 61.164.9.169 //Hangzhou Telecom
  7 1 ms 1 ms <1 ms 61.164.3.113 //Hangzhou Telecom
  8 <1 ms <1 ms <1 ms 220.191.129.129 //Hangzhou Telecom
  9 3 ms 4 ms 3 ms 202.97.39.105 //Anhui Huangshan
 10 3 ms 4 ms 3 ms 202.97.33.74 //Beijing
 11 185 ms 167 ms 168 ms 202.97.51.194 // Nanning, Guangxi
 12 171 ms 170 ms 172 ms 202.97.49.97 //Beijing
 13 176 ms 175 ms 173 ms gige-g5-9.core1.sjc2.he.net [64.62.142.157] // New York, USA
 14 177 ms 174 ms 176 ms 10gigabitethernet1-1.core1.fmt1.he.net [72.52.92.109] // California, USA
 15 174 ms 177 ms 173 ms 10gige1-1.fmt1.egihosting.com [64.71.129.70] //
 16 172 ms 172 ms 172 ms 72.13.80.26 // California, USA
 17 175 ms 175 ms 176 ms 68.68.99.173 //Arrived at target host (California, USA)
 
Trace complete.

The above ip address comes from: http://www.ip2location.com/

linux之traceroute

The command parameters of
Traceroute: The usage of Traceroute is: Traceroute [options] <IP-address or domain-name> [data size]
The content of [options] is:

      -d Use socket-level debugging.
      -f<live value> Set the size of the live value TTL of the first detection packet.
      -F Set the do not leave bit.
      -g<gateway> Set source routing gateway, up to 8 can be set.
      -i <network interface> Send packets using the specified network interface.
      -I Use ICMP responses instead of UDP data messages.
      -m<survival value> Set the maximum survival value TTL size of the detection packet.
      -n Use the IP address directly instead of the hostname.
      -p <communication port> Set the communication port of the UDP transport protocol.
(default is 33434)

    -q Set the number of TTL tests (default is 3)
      -r Ignore the ordinary Routing Table and send the data packet directly to the remote host.
      -s<source address> Set the IP address of the data packet sent by the local host.
      -t <service type> Set the TOS value of the detection packet.
      -v Displays the execution process of the command in detail.
      -w<timeout seconds> Set the time to wait for the remote host to report back.
      -x Enables or disables correctness checking of packets. 

    [data size]: The data byte length of each test packet (default is 38)

Because my vps system is CentOS, but the traceroute software is not installed, I need to install it myself

[root@localhost ~]# traceroute //没有这个命令

-bash: traceroute: command not found

采用yum进行安装(安装完成后可以输入traceroute看到更多可选参数):
yum_install_traceroute.jpg大小: 70.67 K尺寸: 254 x 300浏览: 86 次点击打开新窗口浏览全图
测试下从美国到杭州某网络公司服务器:
tracert eb.jpg大小: 70.31 K尺寸: 300 x 210浏览: 87 次点击打开新窗口浏览全图
测试下从vps到牛人张宴博客的路由跳转:
tracert s135.jpg大小: 15.19 K尺寸: 300 x 34浏览: 40 次点击打开新窗口浏览全图
看来是在同一个机房里的机器,只是出口ip不一样。

(二)Traceroute的工作原理
Traceroute最简单的基本用法是:traceroute hostname
Traceroute程序的设计是利用ICMP及IP header的TTL(Time To Live)栏位(field)。首先,traceroute送出一个TTL是1的IP datagram(其实,每次送出的为3个40字节的包,包括源地址,目的地址和包发出的时间标签)到目的地,探测包是UDP格式,由于我们不想目的主机处理它们,因此目的端口设置为一个不可能的值。当路径上的第一个路由器(router)收到这个datagram时,它将TTL减1。此时,TTL变为0了,所以该路由器会将此datagram丢掉,并送回一个「ICMP time exceeded」消息(包括发IP包的源地址,IP包的所有内容及路由器的IP地址),traceroute 收到这个消息后,便知道这个路由器存在于这个路径上,接着traceroute 再送出另一个TTL是2 的datagram,发现第2 个路由器...... traceroute 每次将送出的datagram的TTL 加1来发现另一个路由器,这个重复的动作一直持续到某个datagram 抵达目的地。当datagram到达目的地后,该主机并不会送回ICMP time exceeded消息,因为它已是目的地了,那么traceroute如何得知目的地到达了呢?
Traceroute在送出UDP datagrams到目的地时,它所选择送达的port number 是一个一般应用程序都不会用的号码(30000 以上),所以当此UDP datagram 到达目的地后该主机会送回一个「ICMP port unreachable」的消息,而当traceroute 收到这个消息时,便知道目的地已经到达了。所以traceroute 在Server端也是没有所谓的Daemon 程式。
Traceroute提取发 ICMP TTL到期消息设备的IP地址并作域名解析。每次 ,Traceroute都打印出一系列数据,包括所经过的路由设备的域名及 IP地址,三个包每次来回所花时间。

Traceroute有一个固定的时间等待响应(ICMP TTL到期消息)。如果这个时间过了,它将打印出一系列的*号表明:在这个路径上,这个设备不能在给定的时间内发出ICMP TTL到期消息的响应。然后,Traceroute给TTL记数器加1,继续进行。

以上部分内容转自:http://yp.oss.org.cn/software/show_resource.php?resource_id=1057

(三)什么是Traceroute网关—Traceroute  Gateway 
            一般使用Traceroute(或者是Tracert)是基于一台主机的,但是通常您只能知道以手边的主机为源地址到互联网络上任意一台在线的主机的路由连接质量以及数据传输效率的情况,而使用基于WEB的方式,只要一台主机安装了特定的CGI程序,用户就可以通过这台主机运行相关的程序,执行Traceroute的功能。这台主机我们把它叫做Traceroute网关。Traceroute网关可以帮助用户了解网络的物理与逻辑连接的拓扑情况以及数据传输的效率。如果这种网关足够多,我们就可以方便地了解到各主机之间连接的情况了。

(四)为什么要使用Traceroute?
          1.几乎每一个网上人(尤其是Webmaster)对他们的计算机(或其它设备)与Internet的连接,路由(径),连通时间,速度等都很关心。使用由ChianNetMap组织起来的各地区Webmaster提供的Traceroute网关的服务,将给你一个满意的答案。从你的计算机到任何别的地方,ChinaNetMap(Traceroute)都能提供其间的每个设备(IP地址)及其连通时间。它可以让你画出通过网络的路径。
          2.许多公司和单位都设有或正在设立自己的服务器-尤其是Web服务器。一旦有自己的Web服务器,随着网民数量的日益增加(包括潜在的,没法统计的网民),
你一定很想知道是否他们都能与你连接。你的ISP如何与一个或多个NAP连接,以及他们的连接效率会直接影响到你的连接质量.
          3.在选择ISP,骨干网连接,你站点的主机时,大多数有见识的网民喜欢检查该Site的连接性能及其它是怎样精确的与谁连,连到哪儿。ChinaNetMap(Traceroute)将给你一个完美的答案。

(五)将来Traceroute实现的算法

下面讨论一下Traceroute将来的实现方法,详细信息参考RFC1393。目前Quidway系列路由器上Traceroute应用实现的方法仍然是上面所讲的方法。
将来要定义一个新的IP Traceroute选项,这样源站发出一个数据包(TTL为指定的缺省值,而不是1、2...),就可以使各个路由器返回一个新定义的ICMP Traceroute消息给源站,通过这种方式,只需通过n + 1个数据包,源站就可以记录下它发送出去的数据包所经过的路由器。
IP Traceroute 选项的格式为:
                             16              24
   +-+-+-+-+-+-+-+-+---------------+---------------+---------------+
   |F| C |  Number |    Length             ID Number            |
   +-+-+-+-+-+-+-+-+---------------+---------------+---------------+
       Outbound Hop Count            Return Hop Count        |
   +---------------+---------------+---------------+---------------+
                      Originator IP Address                     |
   +---------------+---------------+---------------+---------------+
 F(拷贝标志)
  0 仅把该选项拷贝到第一个分片中(通常设置为0)
  1 该选项应该被拷贝到所有的分片中
  C(选项类)
  2 调试和测量
  Number(选项号)18 ( F + C + Number = 82 )
  Length 长度
  ID Number 由发送站规定发送出去的数据包的标识符,与IP头中的ID Number域无关。
  Originator IP Address 发送站的IP地址。
  Outbound Hop Count ( OHC )发送的数据包经过的路由器的个数
  Return Hop Count ( RHC ) 返回的数据包经过的路由器的个数

ICMP Traceroute 消息格式:
   +---------------+---------------+---------------+---------------+
      Type         Code               Checksum            |
   +---------------+---------------+---------------+---------------+
            ID Number                     unused             |
   +---------------+---------------+---------------+---------------+
       Outbound Hop Count            Return Hop Count        |
   +---------------+---------------+---------------+---------------+
                        Output Link Speed                       |
   +---------------+---------------+---------------+---------------+
                         Output Link MTU                        |
   +---------------+---------------+---------------+---------------+
   Type 30
   Code
      0 – 发送的数据包成功地向前传送
      1 - 发送的数据包被丢弃,因为没有路由
   Checksum 校验和
   ID Number 接收到的IP Traceroute选项的ID Number的拷贝,与IP头中的ID Number域无关。
   Outbound Hop Count 接收到的IP Traceroute选项的Outbound Hop Count的拷贝
   Return Hop Count 接收到的IP Traceroute选项的Return Hop Count的拷贝
   Output Link Speed 发送或返回的数据包的速率,单位是字节/秒,如果无法确定,此域设置为0
   Output Link MTU 发送或返回的数据包的MTU,单位是字节,如果无法确定,此域为0
源站发出的数据包的OHC设置为0,RHC设置为0xFFFF,0xFFFF可以用来指明这是一个发送的数据包,而不是返回的数据包。当一个路由器收到一个RHC为0xFFFF的数据包时,表明此数据包是源站发送过来的,于是向前传送一个带有IP Traceroute选项的数据包时,同时发送一个ICMP Traceroute 消息给源站,向前传送的数据包的RHC仍是0xFFFF,而OHC加1,向源站返回的数据包的RHC设置为0;当一个路由器收到一个RHC不是0xFFFF的带IP Traceroute选项的数据包时,表明这是一个前方返回的数据包,则该路由器将RHC加1,继续向源站方向发送。各个路由器返回的ICMP报文不包括IP Traceroute 选项。到达目的时,目的地返回一个带有IP Traceroute 选项的IP数据包,但设置RHC为0,而不再是返回一个ICMP报文。
这种算法的缺点是并非所有的路由器都支持。

(六)使用实例:

实例1:traceroute 用法简单、最常用的用法

命令:traceroute www.baidu.com 

输出:

代码如下:
[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
1 192.168.74.2 (192.168.74.2) 2.606 ms 2.771 ms 2.950 ms
2 211.151.56.57 (211.151.56.57) 0.596 ms 0.598 ms 0.591 ms
3 211.151.227.206 (211.151.227.206) 0.546 ms 0.544 ms 0.538 ms
4 210.77.139.145 (210.77.139.145) 0.710 ms 0.748 ms 0.801 ms
5 202.106.42.101 (202.106.42.101) 6.759 ms 6.945 ms 7.107 ms
6 61.148.154.97 (61.148.154.97) 718.908 ms * bt-228-025.bta.net.cn (202.106.228.25) 5.177 ms
7 124.65.58.213 (124.65.58.213) 4.343 ms 4.336 ms 4.367 ms
8 202.106.35.190 (202.106.35.190) 1.795 ms 61.148.156.138 (61.148.156.138) 1.899 ms 1.951 ms
9 * * *
30 * * *
[root@localhost ~]# 

说明:

记录按序列号从1开始,每个纪录就是一跳 ,每跳表示一个网关,我们看到每行有三个时间,单位是 ms,其实就是-q的默认参数。探测数据包向每个网关发送三个数据包后,网关响应后返回的时间;如果您用 traceroute -q 4 www.58.com ,表示向每个网关发送4个数据包。

有时我们traceroute 一台主机时,会看到有一些行是以星号表示的。出现这样的情况,可能是防火墙封掉了ICMP的返回信息,所以我们得不到什么相关的数据包返回数据。

有时我们在某一网关处延时比较长,有可能是某台网关比较阻塞,也可能是物理设备本身的原因。当然如果某台DNS出现问题时,不能解析主机名、域名时,也会 有延时长的现象;您可以加-n 参数来避免DNS解析,以IP格式输出数据。

如果在局域网中的不同网段之间,我们可以通过traceroute 来排查问题所在,是主机的问题还是网关的问题。如果我们通过远程来访问某台服务器遇到问题时,我们用到traceroute 追踪数据包所经过的网关,提交IDC服务商,也有助于解决问题;但目前看来在国内解决这样的问题是比较困难的,就是我们发现问题所在,IDC服务商也不可能帮助我们解决。

实例2:跳数设置

命令:traceroute -m 10 www.baidu.com

输出:

代码如下:
[root@localhost ~]# traceroute -m 10 www.baidu.com
traceroute to www.baidu.com (61.135.169.105), 10 hops max, 40 byte packets
1 192.168.74.2 (192.168.74.2) 1.534 ms 1.775 ms 1.961 ms
2 211.151.56.1 (211.151.56.1) 0.508 ms 0.514 ms 0.507 ms
3 211.151.227.206 (211.151.227.206) 0.571 ms 0.558 ms 0.550 ms
4 210.77.139.145 (210.77.139.145) 0.708 ms 0.729 ms 0.785 ms
5 202.106.42.101 (202.106.42.101) 7.978 ms 8.155 ms 8.311 ms
6 bt-228-037.bta.net.cn (202.106.228.37) 772.460 ms bt-228-025.bta.net.cn (202.106.228.25) 2.152 ms 61.148.154.97 (61.148.154.97) 772.107 ms
7 124.65.58.221 (124.65.58.221) 4.875 ms 61.148.146.29 (61.148.146.29) 2.124 ms 124.65.58.221 (124.65.58.221) 4.854 ms
8 123.126.6.198 (123.126.6.198) 2.944 ms 61.148.156.6 (61.148.156.6) 3.505 ms 123.126.6.198 (123.126.6.198) 2.885 ms
9 * * *
10 * * *
[root@localhost ~]#
实例3:显示IP地址,不查主机名

命令:traceroute -n www.baidu.com

输出:

代码如下:
[root@localhost ~]# traceroute -n www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
1 211.151.74.2 5.430 ms 5.636 ms 5.802 ms
2 211.151.56.57 0.627 ms 0.625 ms 0.617 ms
3 211.151.227.206 0.575 ms 0.584 ms 0.576 ms
4 210.77.139.145 0.703 ms 0.754 ms 0.806 ms
5 202.106.42.101 23.683 ms 23.869 ms 23.998 ms
6 202.106.228.37 247.101 ms * *
7 61.148.146.29 5.256 ms 124.65.58.213 4.386 ms 4.373 ms
8 202.106.35.190 1.610 ms 61.148.156.138 1.786 ms 61.148.3.34 2.089 ms
9 * * *
30 * * *
[root@localhost ~]# traceroute www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
1 211.151.74.2 (211.151.74.2) 4.671 ms 4.865 ms 5.055 ms
2 211.151.56.57 (211.151.56.57) 0.619 ms 0.618 ms 0.612 ms
3 211.151.227.206 (211.151.227.206) 0.620 ms 0.642 ms 0.636 ms
4 210.77.139.145 (210.77.139.145) 0.720 ms 0.772 ms 0.816 ms
5 202.106.42.101 (202.106.42.101) 7.667 ms 7.910 ms 8.012 ms
6 bt-228-025.bta.net.cn (202.106.228.25) 2.965 ms 2.440 ms 61.148.154.97 (61.148.154.97) 431.337 ms
7 124.65.58.213 (124.65.58.213) 5.134 ms 5.124 ms 5.044 ms
8 202.106.35.190 (202.106.35.190) 1.917 ms 2.052 ms 2.059 ms
9 * * *
30 * * *
[root@localhost ~]# 
实例4:探测包使用的基本UDP端口设置6888

命令:traceroute -p 6888 www.baidu.com

输出:

代码如下:
[root@localhost ~]# traceroute -p 6888 www.baidu.com
traceroute to www.baidu.com (220.181.111.147), 30 hops max, 40 byte packets
1 211.151.74.2 (211.151.74.2) 4.927 ms 5.121 ms 5.298 ms
2 211.151.56.1 (211.151.56.1) 0.500 ms 0.499 ms 0.509 ms
3 211.151.224.90 (211.151.224.90) 0.637 ms 0.631 ms 0.641 ms
4 * * *
5 220.181.70.98 (220.181.70.98) 5.050 ms 5.313 ms 5.596 ms
6 220.181.17.94 (220.181.17.94) 1.665 ms !X * *
[root@localhost ~]# 

实例5:把探测包的个数设置为值4

命令:traceroute -q 4 www.baidu.com

输出:

代码如下:
[root@localhost ~]# traceroute -q 4 www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
1 211.151.74.2 (211.151.74.2) 40.633 ms 40.819 ms 41.004 ms 41.188 ms
2 211.151.56.57 (211.151.56.57) 0.637 ms 0.633 ms 0.627 ms 0.619 ms
3 211.151.227.206 (211.151.227.206) 0.505 ms 0.580 ms 0.571 ms 0.569 ms
4 210.77.139.145 (210.77.139.145) 0.753 ms 0.800 ms 0.853 ms 0.904 ms
5 202.106.42.101 (202.106.42.101) 7.449 ms 7.543 ms 7.738 ms 7.893 ms
6 61.148.154.97 (61.148.154.97) 316.817 ms bt-228-025.bta.net.cn (202.106.228.25) 3.695 ms 3.672 ms *
7 124.65.58.213 (124.65.58.213) 3.056 ms 2.993 ms 2.960 ms 61.148.146.29 (61.148.146.29) 2.837 ms
8 61.148.3.34 (61.148.3.34) 2.179 ms 2.295 ms 2.442 ms 202.106.35.190 (202.106.35.190) 7.136 ms
9 * * * *
30 * * * *
[root@localhost ~]# 

实例6:绕过正常的路由表,直接发送到网络相连的主机

命令:traceroute -r www.baidu.com

输出:

代码如下:
[root@localhost ~]# traceroute -r www.baidu.com
traceroute to www.baidu.com (61.135.169.125), 30 hops max, 40 byte packets
connect: 网络不可达
[root@localhost ~]# 

实例7:把对外发探测包的等待响应时间设置为3秒

命令:traceroute -w 3 www.baidu.com

输出:

代码如下:
[root@localhost ~]# traceroute -w 3 www.baidu.com
traceroute to www.baidu.com (61.135.169.105), 30 hops max, 40 byte packets
1 211.151.74.2 (211.151.74.2) 2.306 ms 2.469 ms 2.650 ms
2 211.151.56.1 (211.151.56.1) 0.621 ms 0.613 ms 0.603 ms
3 211.151.227.206 (211.151.227.206) 0.557 ms 0.560 ms 0.552 ms
4 210.77.139.145 (210.77.139.145) 0.708 ms 0.761 ms 0.817 ms
5 202.106.42.101 (202.106.42.101) 7.520 ms 7.774 ms 7.902 ms
6 bt-228-025.bta.net.cn (202.106.228.25) 2.890 ms 2.369 ms 61.148.154.97 (61.148.154.97) 471.961 ms
7 124.65.58.221 (124.65.58.221) 4.490 ms 4.483 ms 4.472 ms
8 123.126.6.198 (123.126.6.198) 2.948 ms 61.148.156.6 (61.148.156.6) 7.688 ms 7.756 ms
9 * * *
30 * * *
[root@localhost ~]# 

参考链接:http://net.chinaunix.net/5/2006/08/24/1140064.shtml

                    http://blog.sina.com.cn/s/blog_53864cba010009z4.html

                    http://yp.oss.org.cn/software/show_resource.php?resource_id=1057

                    http://www.jb51.net/LINUXjishu/152395.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324450186&siteId=291194637