网络排查MTR命令详解

不会命令行的,请使用现成工具

  1. BestTrace 路由跟踪工具
  2. 网页版traceroute

 

mtr命令详解

MTR 是一款强大的网络诊断工具,它集成了 traceroute 和 ping 的功能,并且会收集更多的信息,比如连接状态、可用性等等,在排查网络问题中,非常有用。

mtr(My traceroute)几乎是所有Linux发行版本预装的网络测试工具。其将pingtraceroute的功能合并,所以功能更强大。mtr默认发送ICMP数据包进行链路探测。您也可以通过-u参数来指定使用UDP数据包进行探测。相对于traceroute只会做一次链路跟踪测试,mtr会对链路上的相关节点做持续探测并给出相应的统计信息。所以,mtr能避免节点波动对测试结果的影响,所以其测试结果更正确,建议优先使用。

MTR的安装

在 Linux下可以直接使用 yum 工具(以 CentOS 为例)进行安装:

[root@NextCli.com ~]# yum provides mtr      #查看mtr命令是由哪个安装包提供
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Determining fastest mirrors

 * remi-php80: mirrors.tuna.tsinghua.edu.cn
 * remi-safe: mirrors.tuna.tsinghua.edu.cn
   2:mtr-0.85-7.el7.x86_64 : A network diagnostic tool
   Repo        : base

[root@NextCLi.com ~]# yum install mtr -y     #安装mtr

在 Windows 下,建议安装 besttrace ,这款工具带了 MTR 的功能。

在 macOS 下,建议使用 brew 进行安装。

mtr命令用法

mtr命令最基础的使用很简单,直接使用命令:mtr ip或域名即可

用法说明

# mtr -h
usage: mtr [-BfhvrwctglxspQomniuT46] [--help] [--version] [--report]
                [--report-wide] [--report-cycles=COUNT] [--curses] [--gtk]
                [--csv|-C] [--raw] [--xml] [--split] [--mpls] [--no-dns] [--show-ips]
                [--address interface] [--filename=FILE|-F]
                [--ipinfo=item_no|-y item_no]
                [--aslookup|-z]
                [--psize=bytes/-s bytes] [--order fields]
                [--report-wide|-w] [--inet] [--inet6] [--max-ttl=NUM] [--first-ttl=NUM]
                [--bitpattern=NUM] [--tos=NUM] [--udp] [--tcp] [--port=PORT] [--timeout=SECONDS]
                [--interval=SECONDS] HOSTNAME

常见可选参数说明

  • --report:以报告模式显示输出。
  • --split:将每次追踪的结果分别列出来,而非统计整个结果。
  • --psize:指定ping数据包的大小。
  • --no-dns:不对IP地址做域名反解析。
  • --address:主机有多个IP地址时,设置发送数据包的IP地址。
  • -4:只使用IPv4协议。
  • -6:只使用IPv6协议。

另外,也可以在mtr运行过程中,输入类似如下的字母用于快速切换模式。

  • ?或h:显示帮助菜单。
  • d:切换显示模式。
  • n:启用或禁用DNS域名解析。
  • u:切换使用ICMP或UDP数据包进行探测。

 

MTR 参数详解

 -F, --filename FILE        read hostname(s) from a file
 -4                         use IPv4 only
 -6                         use IPv6 only
 -u, --udp                  use UDP instead of ICMP echo
 -T, --tcp                  use TCP instead of ICMP echo
 -a, --address ADDRESS      bind the outgoing socket to ADDRESS
 -f, --first-ttl NUMBER     set what TTL to start
 -m, --max-ttl NUMBER       maximum number of hops
 -U, --max-unknown NUMBER   maximum unknown host
 -P, --port PORT            target port number for TCP, SCTP, or UDP
 -L, --localport LOCALPORT  source port number for UDP
 -s, --psize PACKETSIZE     set the packet size used for probing
 -B, --bitpattern NUMBER    set bit pattern to use in payload
 -i, --interval SECONDS     ICMP echo request interval
 -G, --gracetime SECONDS    number of seconds to wait for responses
 -Q, --tos NUMBER           type of service field in IP header
 -e, --mpls                 display information from ICMP extensions
 -Z, --timeout SECONDS      seconds to keep probe sockets open
 -r, --report               output using report mode
 -w, --report-wide          output wide report
 -c, --report-cycles COUNT  set the number of pings sent
 -j, --json                 output json
 -x, --xml                  output xml
 -C, --csv                  output comma separated values
 -l, --raw                  output raw format
 -p, --split                split output
 -t, --curses               use curses terminal interface
     --displaymode MODE     select initial display mode
 -n, --no-dns               do not resove host names
 -b, --show-ips             show IP numbers and host names
 -o, --order FIELDS         select output fields
 -y, --ipinfo NUMBER        select IP information in output
 -z, --aslookup             display AS number
 -h, --help                 display this help and exit
 -v, --version              output version information and exit

示例

[root@NextCLi.com ~]# mtr -rn www.baidu.com
Start: Mon Jul 26 16:33:13 2021
HOST: iZbp1377m2iy8ro0dbnt1kZ     Loss%   Snt   Last   Avg  Best  Wrst StDev
  1.|-- 10.12.208.110              0.0%    10    2.7   2.6   2.5   2.9   0.0
  2.|-- 10.12.208.73              10.0%    10    5.0   4.9   4.1   8.2   1.2
  3.|-- 10.255.101.109             0.0%    10    2.5   3.0   2.4   7.8   1.6
  4.|-- 103.41.142.162             0.0%    10    3.5   3.7   3.4   5.4   0.5
  5.|-- 10.102.46.61               0.0%    10    2.9   3.0   2.9   3.1   0.0
  6.|-- 115.238.21.14              0.0%    10    3.1   3.0   3.0   3.1   0.0
  7.|-- 220.191.199.73             0.0%    10    8.1   6.7   6.5   8.1   0.5
  8.|-- 202.97.33.145             30.0%    10   13.9  14.5  13.9  15.9   0.6
  9.|-- 58.213.95.98              80.0%    10   14.8  14.8  14.8  14.9   0.0
 10.|-- 58.213.95.130             90.0%    10   14.6  14.6  14.6  14.6   0.0
 11.|-- 58.213.96.78               0.0%    10   13.0  12.9  12.8  13.0   0.0
 12.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 13.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 14.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 15.|-- ???                       100.0    10    0.0   0.0   0.0   0.0   0.0
 16.|-- 180.101.49.12              0.0%    10   14.0  14.0  14.0  14.0   0.0

返回结果说明

默认配置下,返回结果中各数据列的说明如下:

  • 第一列(Host):节点IP地址和域名。按n键可切换显示。
  • 第二列(Loss%):节点丢包率。
  • 第三列(Snt):每秒发送数据包数。默认值是10,可以通过-c参数指定。
  • 第四列(Last):最近一次的探测延迟。
  • 第五、六、七列(Avg、Best、Worst):分别是探测延迟的平均值、最小值和最大值。
  • 第八列(StDev):标准偏差,越大说明相应节点越不稳定。

Loss%(丢包率)的判断

任一节点的Loss%(丢包率)如果不为零,则说明这一跳网络可能存在问题。导致相应节点丢包的原因通常有以下两种:

  • 运营商基于安全或性能需求,限制了节点的ICMP发送速率,导致丢包。
  • 节点确实存在异常,导致丢包。

结合异常节点及其后续节点的丢包情况,并参见以下内容,判定丢包原因。

  • 如果随后节点均没有丢包,则通常表示异常节点丢包是由于运营商策略限制所致。可以忽略相关丢包。
  • 如果随后节点也出现丢包,则通常说明异常节点确实存在网络异常,导致丢包。
  • 另外,上述两种情况可能同时发生,即相应节点既存在策略限速,又存在网络异常。对于这种情况,如果异常节点及其后续节点连续出现丢包,而且各节点的丢包率不同,则通常以最后几跳的丢包率为准。

关于Timeouts

  • 有时可能看到mtr输出结果中有(???),这可能是一些路由器将ICMP丢弃和没有应答产生超时导致的,或者是返回线路有问题。

  • 超时不一定是丢包的指示。 数据包仍然可以到达它们的目的地,而没有明显的数据包丢失或延迟。 超时可能是由于路由器为了QoS(quality of service)的目的丢弃数据包,或者可能是由于返回路由的某些问题导致的超时。

命令参考实例

使用-r参数显示报告,默认是动态显示的:

mtr -r www.badu.com

使用-c参数设置每秒发送数据包数量:

mtr -r -c 30 www.baidu.com

使用-s参数指定ping数据包的大小:

mtr -r -c 30 -s 1024 www.baidu.com

扩展阅读:

官网:https://www.bitwizard.nl/mtr/

Linode 帮助文档发布的一篇很详细的教程,国内不少教程直接翻译自这篇:https://www.linode.com/docs/networking/diagnostics/diagnosing-network-issues-with-mtr/

此文章对您是否有帮助? 30 用户发现这个很有用 (30 投票)