ToolkitX
知识库工具箱

网络诊断工具

ping, traceroute, tcpdump, wireshark

20min·进阶

01. ping——排障起手式

ping 是你排查网络问题的第一招。发 ICMP 包看能不能收到回复。能通说明网络层没问题,不通往下查。就像喊一声「喂——」看对方回不回。
bash
# 基础
ping -c 4 google.com        # 发 4 个包
ping -i 0.2 google.com      # 快速模式

# 关注:time(延迟,越小越好)、packet loss(丢包率,必须 0%)

02. traceroute / mtr——追踪路径

ping 只告诉你通不通,traceroute 告诉你「怎么通的」——经过哪些路由器,每跳花多久。mtr 是增强版,实时持续追踪。
bash
traceroute google.com
mtr google.com              # 实时交互式追踪

# 关注:每跳 IP 和延迟,* * * 可能被防火墙屏蔽

03. curl——HTTP 瑞士军刀

curl 是你跟 HTTP 打交道最常用的兵器。调试 API、测试接口、分析耗时全用它。
bash
# 详细看全过程
curl -v https://httpbin.org/get

# POST JSON
curl -X POST https://httpbin.org/post \
-H "Content-Type: application/json" \
-d '{"key": "value"}'

# 分析各环节耗时(定位瓶颈)
curl -o /dev/null -s -w \
"DNS: %{time_namelookup}s\nTCP: %{time_connect}s\nTLS: %{time_appconnect}s\n首字节: %{time_starttransfer}s\n总: %{time_total}s\n" \
https://example.com
curl -w 能抽出各个环节的耗时,帮你精准定位是 DNS 慢、TLS 握手慢还是服务器处理慢。

04. tcpdump / Wireshark——抓包大法

当问题找不到头绪时,抓包是最后的手段。tcpdump 命令行版,Wireshark 图形化分析。能看到每个包的完整细节。
bash
# tcpdump 常用
sudo tcpdump -i eth0 port 80 -nn
sudo tcpdump -i eth0 host 192.168.1.1
sudo tcpdump -i eth0 -w capture.pcap  # 存文件给 Wireshark
生产环境抓包注意加过滤和 -c 限数,不然几秒就能撑爆磁盘。

05. ss——看端口和连接

ss 是 netstat 的继任者,又快又好用。
bash
# 看所有监听
ss -tlnp                    # TCP 监听
ss -ulnp                    # UDP 监听
ss -s                       # 连接统计

# 谁占着端口
lsof -i :80

# Nginx 用了哪些端口
lsof -i -P -n | grep nginx

知识测验

1/5正确 0

ping 用的是什么协议?