mtr命令詳解


MTR 是一款強大的網絡診斷工具,它集成了 traceroute 和 ping 的功能,並且會收集更多的信息,比如連接狀態、可用性等等,在排查網絡問題中,非常有用。

mtr(My traceroute)幾乎是所有Linux發行版本預裝的網絡測試工具。其將pingtraceroute的功能合並,所以功能更強大。mtr默認發送ICMP數據包進行鏈路探測。您也可以通過-u參數來指定使用UDP數據包進行探測。相對於traceroute只會做一次鏈路跟蹤測試,mtr會對鏈路上的相關節點做持續探測並給出相應的統計信息。所以,mtr能避免節點波動對測試結果的影響,所以其測試結果更正確,建議優先使用。

MTR的安裝

在 Linux下可以直接使用 yum 工具(以 CentOS 為例)進行安裝:

[root@iZbp1377m2iy8ro0dbnt1kZ ~]# 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@iZbp1377m2iy8ro0dbnt1kZ ~]# 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@iZbp1377m2iy8ro0dbnt1kZ ~]# 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/

使用ping命令丟包或不通時的鏈路測試方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM