> For the complete documentation index, see [llms.txt](https://gitbook.fantasticmao.cn/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.fantasticmao.cn/tech/wang-luo-xie-yi/zhen-duan-gong-ju.md).

# 诊断工具

## curl

`curl` 命令是一个从服务器传输数据，或者向服务器传输数据的工具，支持多种协议：DICT、FILE、FTP、FTPS、GOPHER、HTTP、HTTPS、IMAP、IMAPS、LDAP、LDAPS、MQTT、POP3、POP3S、RTMP、RTMPS、RTSP、SCP、SFTP、SMB、SMBS、SMTP、SMTPS、TELNET、TFTP。`curl` 命令被设计成无需用户交互即可工作的。

更多信息请见 [curl(1) — Linux manual page](https://man7.org/linux/man-pages/man1/curl.1.html)。

```bash
ubuntu:~$ curl https://httpbin.org/get
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.58.0",
    "X-Amzn-Trace-Id": "Root=1-629c984a-2e1c3936379071cc3f9b3fc4"
  },
  "origin": "121.5.122.78",
  "url": "https://httpbin.org/get"
}
ubuntu:~$ curl -X POST -d 'name=hello' https://httpbin.org/post
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "name": "hello"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "10",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.58.0",
    "X-Amzn-Trace-Id": "Root=1-629c98c0-64e567ec62638cda249d4ed5"
  },
  "json": null,
  "origin": "121.5.122.78",
  "url": "https://httpbin.org/post"
}
```

## 查看 ip 信息

```bash
ubuntu:~$ curl -L cip.cc
IP	: 60.12.1.178
地址	: 中国  浙江  杭州
运营商	: 联通

数据二	: 浙江省杭州市 | 联通

数据三	: 中国浙江省杭州市 | 联通

URL	: http://www.cip.cc/60.12.1.178
ubuntu:~$
ubuntu:~$ curl -L ipinfo.io
{
  "ip": "60.12.1.178",
  "city": "Hangzhou",
  "region": "Zhejiang",
  "country": "CN",
  "loc": "30.2936,120.1614",
  "org": "AS4837 CHINA UNICOM China169 Backbone",
  "timezone": "Asia/Shanghai",
  "readme": "https://ipinfo.io/missingauth"
}
```

## ss

`ss` 命令被用于输出 Linux 套接字的统计信息。它显示的信息与 `netstat` 命令类似，并且可以显示更多的 Tcp 和状态信息。

更多信息请见 [ss(8) — Linux manual page](https://man7.org/linux/man-pages/man8/ss.8.html)。

```bash
ubuntu:~$ sudo ss -lntp
State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port
LISTEN   0         128                 0.0.0.0:80               0.0.0.0:*        users:(("nginx",pid=10113,fd=12),("nginx",pid=1184,fd=12))
LISTEN   0         128           127.0.0.53%lo:53               0.0.0.0:*        users:(("systemd-resolve",pid=943,fd=13))
LISTEN   0         128                 0.0.0.0:22               0.0.0.0:*        users:(("sshd",pid=1094,fd=3))
LISTEN   0         128                 0.0.0.0:443              0.0.0.0:*        users:(("nginx",pid=10113,fd=11),("nginx",pid=1184,fd=11))
LISTEN   0         128                       *:9100                   *:*        users:(("node_exporter",pid=1042,fd=3))
LISTEN   0         128                       *:1200                   *:*        users:(("frps",pid=1071,fd=9))
LISTEN   0         128                       *:8080                   *:*        users:(("frps",pid=1071,fd=8))
LISTEN   0         128                    [::]:80                  [::]:*        users:(("nginx",pid=10113,fd=13),("nginx",pid=1184,fd=13))
LISTEN   0         100                       *:1234                   *:*        users:(("java",pid=3640,fd=21))
LISTEN   0         128                       *:8088                   *:*        users:(("node",pid=2249,fd=18))
LISTEN   0         128                       *:7000                   *:*        users:(("frps",pid=1071,fd=3))
LISTEN   0         128                       *:9508                   *:*        users:(("frps",pid=1071,fd=10))
```

## nslookup

`nslookup`（Name Server Lookup）命令用来从 DNS 中查询域名对应的 ip 地址和其它 DNS 记录信息。

```bash
ubuntu:~$ nslookup 3.cn
Server:		127.0.0.53
Address:	127.0.0.53#53

Non-authoritative answer:
Name:	3.cn
Address: 106.39.164.153
Name:	3.cn
Address: 111.13.28.27
```

## ping

`ping` 命令用来测试数据包能否通过 IP 网络发送到目标主机。

`ping` 命令的运行原理是向目标主机发送 [ICMP](https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol) 协议的 ECHO\_REQUEST 数据包，等待接收响应的 ECHO\_RESPONSE 数据包，然后按响应时间和成功次数估算丢包率和延时。

```bash
ubuntu:~$ ping 3.cn -t 4
PING 3.cn (106.39.164.153): 56 data bytes
64 bytes from 106.39.164.153: icmp_seq=0 ttl=50 time=39.883 ms
64 bytes from 106.39.164.153: icmp_seq=1 ttl=50 time=43.923 ms
64 bytes from 106.39.164.153: icmp_seq=2 ttl=50 time=39.428 ms
64 bytes from 106.39.164.153: icmp_seq=3 ttl=50 time=47.891 ms

--- 3.cn ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 39.428/42.781/47.891/3.430 ms
```

## traceroute

`traceroute` 命令用来显示数据包在 IP 网络上发送到目标主机的路由信息。

更多信息请见 [traceroute(8) — Linux manual page](https://man7.org/linux/man-pages/man8/traceroute.8.html)。

## tcptraceroute

`tcptraceroute` 命令的效果等同于 `traceroute -T` 命令。

## mtr

`mtr` 命令是一个将 `traceroute` 和 `ping` 功能结合在一起的网络诊断工具。

更多信息请见 [MTR 官方文档](https://www.bitwizard.nl/mtr/)。

```bash
                             My traceroute  [v0.95]
ubuntu (172.19.159.133) -> 121.5.122.78 (121.5.122.78)2023-07-28T16:54:42+0800
Keys:  Help   Display mode   Restart statistics   Order of fields   quit
                                       Packets               Pings
 Host                                Loss%   Snt   Last   Avg  Best  Wrst StDev
 1. 172.19.159.254                    0.0%    14    3.4  53.7   3.4 272.2  72.9
 2. (waiting for reply)
 3. 60.12.1.177                       0.0%    14    4.8  15.7   4.8 115.1  28.8
 4. 101.68.76.73                      0.0%    14    5.0  12.2   4.1  33.1  10.7
 5. 124.160.233.157                  64.3%    14    6.0  50.0   4.7 226.7  98.8
 6. 219.158.114.145                  92.3%    14  159.2 159.2 159.2 159.2   0.0
 7. (waiting for reply)
 8. 220.196.197.162                  30.8%    14   79.5  81.5  12.5 219.5  65.4
 9. (waiting for reply)
10. (waiting for reply)
11. (waiting for reply)
12. (waiting for reply)
13. 121.5.122.78                      0.0%    13   81.6  84.3  15.2 358.9  86.7
```

## tcpdump

`tcpdump` 命令会打印经过网卡的数据包。

常用基础参数：

* `-i` 指定监听的网卡接口，例如 `eth0`、`ens18`，使用 `any` 可以抓取所有网卡；
* `-n` 不将主机名、端口号换为别名或域名，直接显示 IP 和数字端口，大幅提高查看速度；
* `-nn` 在 `-n` 的基础上，进一步不转换协议名和服务端口，例如显示 `80` 而非 `http`；
* `-c` 抓取指定数量的数据包后自动停止；
* `-w` 将抓包数据保存为 `.pcap` 格式的文件，方便导入 `Wireshark` 等图形界面工具中进行深度分析；
* `-r` 读取已保存的 `.pcap` 抓包文件；
* `-s` 设置每个数据包的抓取长度（单位为字节），默认是 `262144` 字节，使用 `0` 表示抓取完整的数据包；
* `-tttt` 以更加完整、易读取的格式，打印时间戳，例如 `2026-01-01 12:00:00.000000`；
* `-v` / `-vv` / `-vvv` 增加输出信息的详情程度，显示更多报文头部信息。

经典实战命令：

* `tcpdump -i eth0 -w dump.pcapng` 命令表示把经过 `eth0` 网卡上的数据包保存到 `dump.pcapng` 文件中；
* `tcpdump host 192.168.1.100` 命令表示抓取所有源地址或目的地址为 `192.168.1.100` 的数据包，`tcpdump src host 192.168.1.100` 命令表示抓取源地址为 `192.168.1.100` 的数据包，`tcpdump dst host 192.168.1.100` 命令表示抓取目的地址为 `192.168.1.100` 的数据包；
* `tcpdump port 80` 命令表示抓取通过 `80` 端口的所有数据包，`tcpdump dst port 443` 命令表示抓取目的端口为 443 的数据包；
* `tcpdump icmp` 命令表示抓取 ICMP 协议数据包，`tcpdump tcp` 表示抓取 TCP 协议数据包；
* `tcpdump src host 192.168.1.100 and port 80` 命令表示抓取源 IP 是 `192.168.1.100` 且端口是 `80` 的数据包，`tcpdump port not 22` 命令表示抓取端口不是 `22` 的数据包。

更多信息请见 [tcpdump(1) — Linux manual page](https://man7.org/linux/man-pages/man1/tcpdump.1.html)。

## iperf3

`iperf3` 是一种压测 IP 网络中最大带宽的工具。

`iperf3` 支持调整时序、协议、缓冲区等等相关的各种参数，对于每个测试用例，它都会输出包含吞吐量 / 比特率、丢失等等参数的测试结果。

```bash
ubuntu:~$ iperf3 -s -p 5201 -f Mbits
-----------------------------------------------------------
Server listening on 5201
-----------------------------------------------------------
Accepted connection from 192.168.0.67, port 63981
[  5] local 192.168.0.102 port 5201 connected to 192.168.0.67 port 63982
[  8] local 192.168.0.102 port 5201 connected to 192.168.0.67 port 63983
[ ID] Interval           Transfer     Bitrate
[  5]   0.00-1.00   sec  35.3 MBytes  35.3 MBytes/sec
[  8]   0.00-1.00   sec  37.8 MBytes  37.8 MBytes/sec
[SUM]   0.00-1.00   sec  73.2 MBytes  73.2 MBytes/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[  5]   1.00-2.00   sec  44.2 MBytes  44.2 MBytes/sec
[  8]   1.00-2.00   sec  44.5 MBytes  44.5 MBytes/sec
[SUM]   1.00-2.00   sec  88.7 MBytes  88.7 MBytes/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[  5]   2.00-3.00   sec  45.2 MBytes  45.2 MBytes/sec
[  8]   2.00-3.00   sec  47.1 MBytes  47.1 MBytes/sec
[SUM]   2.00-3.00   sec  92.3 MBytes  92.3 MBytes/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[  5]   3.00-3.02   sec   584 KBytes  37.4 MBytes/sec
[  8]   3.00-3.02   sec   588 KBytes  37.7 MBytes/sec
[SUM]   3.00-3.02   sec  1.14 MBytes  75.2 MBytes/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate
[  5]   0.00-3.02   sec   125 MBytes  41.6 MBytes/sec                  receiver
[  8]   0.00-3.02   sec   130 MBytes  43.1 MBytes/sec                  receiver
[SUM]   0.00-3.02   sec   255 MBytes  84.7 MBytes/sec                  receiver
```

```bash
ubuntu:~$ iperf3 -c 192.168.0.102 -p 5201 -t 3 -P 2
Connecting to host 192.168.0.102, port 5201
[  7] local 192.168.0.67 port 63982 connected to 192.168.0.102 port 5201
[  9] local 192.168.0.67 port 63983 connected to 192.168.0.102 port 5201
[ ID] Interval           Transfer     Bitrate         Retr  Cwnd          RTT
[  7]   0.00-1.00   sec  38.2 MBytes   320 Mbits/sec    0   2.18 MBytes   57ms
[  9]   0.00-1.00   sec  40.6 MBytes   341 Mbits/sec    0   3.00 MBytes   87ms
[SUM]   0.00-1.00   sec  78.8 MBytes   661 Mbits/sec    0
- - - - - - - - - - - - - - - - - - - - - - - - -
[  7]   1.00-2.00   sec  43.9 MBytes   368 Mbits/sec    0   2.20 MBytes   59ms
[  9]   1.00-2.00   sec  43.9 MBytes   368 Mbits/sec    0   2.67 MBytes   63ms
[SUM]   1.00-2.00   sec  87.9 MBytes   736 Mbits/sec    0
- - - - - - - - - - - - - - - - - - - - - - - - -
[  7]   2.00-3.00   sec  45.7 MBytes   383 Mbits/sec    0   2.19 MBytes   48ms
[  9]   2.00-3.00   sec  48.5 MBytes   408 Mbits/sec    0   2.03 MBytes   38ms
[SUM]   2.00-3.00   sec  94.2 MBytes   791 Mbits/sec    0
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bitrate         Retr
[  7]   0.00-3.00   sec   128 MBytes   357 Mbits/sec    0             sender
[  7]   0.00-3.02   sec   125 MBytes   349 Mbits/sec                  receiver
[  9]   0.00-3.00   sec   133 MBytes   372 Mbits/sec    0             sender
[  9]   0.00-3.02   sec   130 MBytes   362 Mbits/sec                  receiver
[SUM]   0.00-3.00   sec   261 MBytes   729 Mbits/sec    0             sender
[SUM]   0.00-3.02   sec   255 MBytes   710 Mbits/sec                  receiver

iperf Done.
```
