traceroute命令
traceroute (Windows 系统下是tracert) 命令利用ICMP 协议定位您的计算机和目标计算机之间的所有路由器。TTL 值可以反映数据包经过的路由器或网关的数量,通过操纵独立ICMP 呼叫报文的TTL 值和观察该报文被抛弃的返回信息,traceroute命令能够遍历到数据包传输路径上的所有路由器
traceroute命令语法
traceroute [ -adDeFISnrvx ] [ -f first_ttl ] [ -g gateway ]
[ -i iface ] [ -M first_ttl ]
[ -m max_ttl ] [ -P proto ] [ -p port ]
[ -q nqueries ] [ -s src_addr ] [ -t tos ]
[ -w waittime ] [ -A as_server ] [ -z pausemsecs ]
host [ packetlen ]
互联网是由网关连接在一起的大型复杂的网络硬件集合。跟踪某人的数据包遵循的路由(或找到丢弃该数据包的错误网关)可能很困难。 Traceroute利用IP协议的“生存时间”字段,并尝试从每个网关到某个主机的路径中引发ICMP TIME_EXCEEDED响应。
唯一必需的参数是目标主机名或IP地址。默认的探测数据报长度为40字节,但是可以通过在目标主机名之后指定一个数据包长度(以字节为单位)来增加。
参数解释
Option | Option |
---|---|
-a | Turn on AS# lookups for each hop encountered. |
-A | Turn on AS# lookups and use the given server instead of the default. |
-e | Firewall evasion mode. Use fixed destination ports for UDP and TCP probes. The destination port does NOT increment with each packet sent. |
-f | Set the initial time-to-live used in the first outgoing probe packet. |
-F | Set the “don’t fragment” bit. |
-d | Enable socket level debugging. |
-D | When an ICMP response to our probe datagram is received, print the differences between the transmitted packet and the packet quoted by the ICMP response. A key showing the location of fields within the transmitted packet is printed, followed by the original packet in hex, followed by the quoted packet in hex. Bytes that are unchanged in the quoted packet are shown as underscores. Note, the IP checksum and the TTL of the quoted packet are not expected to match. By default, only one probe per hop is sent with this option. |
-g | Specify a loose source route gateway (8 maximum). |
-i | Specify a network interface to obtain the source IP address for outgoing probe packets. This is normally only useful on a multi-homed host. (See the -s flag for another way to do this.) |
-I | Use ICMP ECHO instead of UDP datagrams. (A synonym for “-P icmp”). |
该程序尝试通过启动具有较小ttl(生存时间)的UDP探测数据包,然后侦听网关发出的ICMP超时答复,来跟踪IP数据包到达某些Internet主机的路由。我们从ttl为1开始探测,然后再增加1,直到得到ICMP端口不可达(这意味着我们必须托管)或达到最大值(默认为net.inet.ip.ttl),hops&can可以通过-m标志进行更改)。在每个ttl设置中发送三个探针(用-q标志更改),并打印一行,显示ttl,网关地址和每个探针的往返时间。如果探测答案来自不同的网关,则将打印每个响应系统的地址。如果5秒钟内没有响应。超时间隔(由-w标志更改),将为该探针打印“ *”。
实例操作
要了解到某计算机如 www.baidu.com 中间经过了哪些节点(路由器)及其它状态,可使用 traceroute www.baidu.com
命令,查看反馈的信息,了解节点的个数。
traceroute www.baidu.com
traceroute: Warning: www.baidu.com has multiple addresses; using 14.215.177.39
traceroute to www.a.shifen.com (14.215.177.39), 64 hops max, 52 byte packets
1 10.160.255.254 (10.160.255.254) 7.256 ms 5.190 ms 2.750 ms
2 172.19.2.2 (172.19.2.2) 3.307 ms 2.160 ms 2.476 ms
3 183.64.62.161 (183.64.62.161) 3.751 ms 5.975 ms 5.995 ms
4 222.176.89.77 (222.176.89.77) 7.415 ms
222.176.39.221 (222.176.39.221) 4.939 ms
222.176.89.65 (222.176.89.65) 10.163 ms
可通过网站 http://ip.cn 查看这些节点位于何处,是哪个公司的,大致清楚本机到百度服务器之间的路径。
ping.pe 这个网站可以探测从全球主要的 ISP 到某站点如 https://qige.io 的线路状态,当然也包括各线路到该主机的路由情况。请使用浏览器访问 http://ping.pe/www.baidu.com 进行了解,大致如下图所示:
相关分享
提供几个全球地图,可视化的显示追踪路径也值得你前往(因为使用了 Google 地图,需要科学上网)
- https://visualtraceroute.net/
- http://en.dnstools.ch/visual-traceroute.html
- https://gsuite.tools/traceroute
bugs
使用UDP以外的协议时,功能会降低。特别是,最后一个数据包经常看起来会丢失,因为即使到达目的地主机,也无法知道,因为没有ICMP消息回传。在TCP情况下,traceroute应该从目标主机(或正在过滤数据包的中间路由器)侦听RST,但这尚未实现。