通过 traceroute 命令排查网络问题
Linux 中使用 traceroute Windows 使用 tracert MacOS 使用 tcptraceroute、traceroute
简单了解 traceroute 命令:https://www.cnblogs.com/peida/archive/2013/03/07/2947326.html
<span style="color: red;">使用这个命令可以让我们知道从本机到目标主机所走的路径。</span>
每个数据包从 source 到 destination 走的路径不一定会一样。
一、命令格式
traceroute [参数] [主机]
traceroute [-dFlnrvx][-f<存活数值>][-g<网关>...][-i<网络界面>][-m<存活数值>][-p<通信端口>][-s<来源地址>][-t<服务类型>][-w<超时秒数>][主机名称或IP地址][数据包大小]
Linux
[root@VM-16-10-centos ~]# traceroute --help
Usage:
traceroute [ -46dFITnreAUDV ] [ -f first_ttl ] [ -g gate,... ] [ -i device ] [ -m max_ttl ] [ -N squeries ] [ -p port ] [ -t tos ] [ -l flow_label ] [ -w waittime ] [ -q nqueries ] [ -s src_addr ] [ -z sendwait ] [ --fwmark=num ] host [ packetlen ]
Options:
-4 Use IPv4
-6 Use IPv6
-d --debug Enable socket level debugging
-F --dont-fragment Do not fragment packets
-f first_ttl --first=first_ttl
Start from the first_ttl hop (instead from 1)
-g gate,... --gateway=gate,...
Route packets through the specified gateway
(maximum 8 for IPv4 and 127 for IPv6)
-I --icmp Use ICMP ECHO for tracerouting
-T --tcp Use TCP SYN for tracerouting (default port is 80)
-i device --interface=device
Specify a network interface to operate with
-m max_ttl --max-hops=max_ttl
Set the max number of hops (max TTL to be
reached). Default is 30
-N squeries --sim-queries=squeries
Set the number of probes to be tried
simultaneously (default is 16)
-n Do not resolve IP addresses to their domain names
-p port --port=port Set the destination port to use. It is either
initial udp port value for "default" method
(incremented by each probe, default is 33434), or
initial seq for "icmp" (incremented as well,
default from 1), or some constant destination
port for other methods (with default of 80 for
"tcp", 53 for "udp", etc.)
-t tos --tos=tos Set the TOS (IPv4 type of service) or TC (IPv6
traffic class) value for outgoing packets
-l flow_label --flowlabel=flow_label
Use specified flow_label for IPv6 packets
-w waittime --wait=waittime
Set the number of seconds to wait for response to
a probe (default is 5.0). Non-integer (float
point) values allowed too
-q nqueries --queries=nqueries
Set the number of probes per each hop. Default is
3
-r Bypass the normal routing and send directly to a
host on an attached network
-s src_addr --source=src_addr
Use source src_addr for outgoing packets
-z sendwait --sendwait=sendwait
Minimal time interval between probes (default 0).
If the value is more than 10, then it specifies a
number in milliseconds, else it is a number of
seconds (float point values allowed too)
-e --extensions Show ICMP extensions (if present), including MPLS
-A --as-path-lookups Perform AS path lookups in routing registries and
print results directly after the corresponding
addresses
-M name --module=name Use specified module (either builtin or external)
for traceroute operations. Most methods have
their shortcuts (`-I' means `-M icmp' etc.)
-O OPTS,... --options=OPTS,...
Use module-specific option OPTS for the
traceroute module. Several OPTS allowed,
separated by comma. If OPTS is "help", print info
about available options
--sport=num Use source port num for outgoing packets. Implies
`-N 1'
--fwmark=num Set firewall mark for outgoing packets
-U --udp Use UDP to particular port for tracerouting
(instead of increasing the port per each probe),
default port is 53
-UL Use UDPLITE for tracerouting (default dest port
is 53)
-D --dccp Use DCCP Request for tracerouting (default port
is 33434)
-P prot --protocol=prot Use raw packet of protocol prot for tracerouting
--mtu Discover MTU along the path being traced. Implies
`-F -N 1'
--back Guess the number of hops in the backward path and
print if it differs
-V --version Print version info and exit
--help Read this help and exit
Arguments:
+ host The host to traceroute to
packetlen The full packet length (default is the length of an IP
header plus 40). Can be ignored or increased to a minimal
allowed value
二、观察 traceroute 命令的结果
1、traceroute www.baidu.com
[root@VM-16-10-centos ~]# traceroute baidu.com
traceroute to baidu.com (39.156.66.10), 30 hops max, 60 byte packets
1 * * *
2 11.82.247.74 (11.82.247.74) 2.056 ms * *
3 10.162.72.105 (10.162.72.105) 5.707 ms 10.162.72.137 (10.162.72.137) 5.778 ms 10.162.72.233 (10.162.72.233) 5.186 ms
4 * * 10.200.77.113 (10.200.77.113) 3.366 ms
5 10.162.5.110 (10.162.5.110) 3.360 ms 120.253.236.34 (120.253.236.34) 2.664 ms 10.162.5.110 (10.162.5.110) 3.461 ms
6 * * *
7 . (117.135.48.197) 15.481 ms . (117.135.48.149) 5.497 ms 4.990 ms
8 221.183.139.1 (221.183.139.1) 4.673 ms 6.053 ms 221.183.53.229 (221.183.53.229) 3.972 ms
9 221.183.37.217 (221.183.37.217) 27.396 ms 221.183.37.133 (221.183.37.133) 29.678 ms 27.878 ms
10 221.183.49.122 (221.183.49.122) 28.226 ms 221.183.49.134 (221.183.49.134) 27.296 ms 28.701 ms
11 111.13.0.174 (111.13.0.174) 30.151 ms 39.156.27.1 (39.156.27.1) 28.461 ms 111.13.188.38 (111.13.188.38) 27.087 ms
12 39.156.27.1 (39.156.27.1) 29.803 ms * *
13 * * *
14 * * *
15 * * *
16 * * *
17 * * *
18 * * *
19 * * *
20 * * *
21 * * *
22 * * *
23 * * *
24 * * *
25 * * *
26 * * *
27 * * *
28 * * *
29 * * *
30 * * *
记录按序列号从1开始,每个纪录就是一跳 ,每跳表示一个网关,我们看到每行有三个时间,单位是 ms。探测数据包向每个网关发送三个数据包后,网关响应后返回的时间; <span style="color: red;">可以通过
-q 4指定向每个网关发生 4 个数据包。</span>
有一些行是以星号表示的。出现这样的情况,可能是防火墙封掉了ICMP的返回信息,所以我们得不到什么相关的数据包返回数据。
有时我们在某一网关处延时比较长,有可能是某台网关比较阻塞,也可能是物理设备本身的原因。当然 如果某台DNS出现问题时,不能解析主机名、域名时,也会 有延时长的现象;您可以加 -n 参数来避免 DNS解析,以IP格式输出数据。
2、traceroute -w 1 -n -T www.baidu.com
<span style="color: red;">macOS 上不支持 -T 参数,可以使用 -I 参数表示使用 ICMP 数据包</span>
-w 1
:设置超时时间为 1s。-n
:不要尝试将IP地址解析为主机名。-T
:指定使用TCP协议而不是默认的UDP协议。在某些情况下,使用TCP可以绕过一些网络设备(如防火墙)的限制。这对于在UDP被阻止的情况下执行Traceroute是有用的。
[root@VM-16-10-centos ~]# traceroute -w 1 -n -T www.baidu.com
traceroute to www.baidu.com (153.3.238.102), 30 hops max, 60 byte packets
1 * * *
2 * * *
3 10.162.72.169 10.191 ms 10.162.72.137 10.331 ms 10.162.73.9 6.138 ms
4 * 10.200.77.137 2.320 ms 2.398 ms
5 220.196.197.170 2.261 ms 220.196.197.162 2.280 ms 10.162.5.110 3.493 ms
6 220.196.197.161 3.669 ms 4.041 ms 220.196.196.13 3.825 ms
7 211.95.32.61 5.017 ms 211.95.32.173 4.923 ms 211.95.32.61 3.993 ms
8 219.158.19.94 8.332 ms 7.888 ms 7.869 ms
9 * * *
10 153.37.96.242 9.729 ms 9.719 ms 9.437 ms
11 * 182.61.255.192 13.703 ms 182.61.255.194 8.878 ms
12 182.61.255.171 25.074 ms 22.741 ms 182.61.255.169 10.991 ms
13 * * *
14 * * *
15 * * *
16 153.3.238.102 9.449 ms 8.616 ms 8.643 ms
三、macos 上无法使用 traceroute 发送 tcp 数据包
https://articles.assembla.com/en/articles/1589335-how-to-use-tcp-traceroute
sudo tcptraceroute baidu.com