网络诊断(上):ping、curl、wget
ping:测试网络连通性
ping 发送 ICMP Echo Request 包到目标主机,测试网络是否可达和延迟。
# 持续 ping(Linux 默认持续,按 Ctrl+C 停止)
ping baidu.com
# 指定次数(-c count)
ping -c 4 baidu.com
# 指定包大小(-s size,不含 ICMP 首部 8 字节)
ping -c 4 -s 56 baidu.com
# 指定间隔(-i interval,秒)
ping -c 4 -i 2 baidu.com
# 禁止分片(-M do,测试 MTU)
ping -M do -s 1472 baidu.com
# IPv6 ping
ping -6 ipv6.google.com
ping 输出解读
PING baidu.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66: icmp_seq=1 ttl=52 time=15.3 ms
64 bytes from 110.242.68.66: icmp_seq=2 ttl=52 time=14.8 ms
64 bytes from 110.242.68.66: icmp_seq=3 ttl=52 time=15.1 ms
64 bytes from 110.242.68.66: icmp_seq=4 ttl=52 time=14.9 ms
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 14.832/15.050/15.312/0.186 ms
| 字段 | 含义 |
|---|---|
| icmp_seq | ICMP 序列号 |
| ttl | Time To Live,剩余跳数 |
| time | 往返时间(RTT) |
| packet loss | 丢包率 |
| rtt min/avg/max | 最小/平均/最大延迟 |
TTL 的隐藏信息
TTL 初始值通常是 64(Linux/Mac)、128(Windows)、255(某些路由器)。通过 64 - ttl 可以估算经过的路由器跳数。
curl:网络请求瑞士军刀
curl(Client URL)是功能最强大的 HTTP/HTTPS 请求工具,支持各种协议和选项。
# 简单 GET 请求
curl http://example.com
# 保存响应到文件(-o output)
curl -o page.html http://example.com
# 跟随重定向(-L location)
curl -L http://bit.ly/xxx
# 显示详细过程(-v verbose)
curl -v http://example.com
# 只显示响应头(-I head)
curl -I http://example.com
# POST 请求(-d data)
curl -X POST -d "name=john&age=20" http://api.example.com/user
# POST JSON 数据
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name":"john","age":20}' \
http://api.example.com/user
# 下载文件并显示进度(-O 使用远程文件名)
curl -O http://example.com/file.zip
# 断点续传(-C continue)
curl -C - -O http://example.com/large-file.zip
# 使用代理
curl -x http://proxy:8080 http://example.com
curl -v 输出解读
curl -v http://example.com
# * Trying 93.184.216.34:80...
# * Connected to example.com (93.184.216.34) port 80 (#0)
# > GET / HTTP/1.1
# > Host: example.com
# > User-Agent: curl/7.68.0
# > Accept: */*
# >
# * Mark bundle as not supporting multiuse
# < HTTP/1.1 200 OK
# < Content-Type: text/html; charset=UTF-8
# < Content-Length: 1256
# <
# <!doctype html>
# ...
| 符号 | 含义 |
|---|---|
* | curl 自身的操作信息 |
> | 发送给服务器的请求头 |
< | 服务器返回的响应头 |
| 无符号 | 响应体内容 |
wget:文件下载工具
wget(Web Get)专注于文件下载,支持递归下载、断点续传、后台下载。
# 简单下载
wget http://example.com/file.zip
# 指定输出文件名
wget -O myfile.zip http://example.com/file.zip
# 后台下载(-b background)
wget -b http://example.com/large-file.zip
# 断点续传(-c continue)
wget -c http://example.com/large-file.zip
# 限制下载速度(--limit-rate)
wget --limit-rate=500k http://example.com/file.zip
# 递归下载网站(-r recursive,慎用!)
wget -r -l 2 http://example.com/
# 下载时显示进度条(默认)
wget http://example.com/file.zip
# 输出:
# --2024-01-15 10:00:00-- http://example.com/file.zip
# Resolving example.com... 93.184.216.34
# Connecting to example.com|93.184.216.34|:80... connected.
# HTTP request sent, awaiting response... 200 OK
# Length: 1048576 (1.0M) [application/zip]
# Saving to: 'file.zip'
# file.zip 100%[===================>] 1.00M 2.50MB/s in 0.4s
curl vs wget 对比
| 场景 | 推荐工具 | 原因 |
|---|---|---|
| 简单下载文件 | wget | 默认显示进度,断点续传更友好 |
| 调试 HTTP 请求 | curl | -v 显示完整请求/响应过程 |
| POST/PUT/DELETE | curl | 支持各种 HTTP 方法 |
| 处理 Cookie/Session | curl | -b 和 -c 选项 |
| 上传文件 | curl | -F 表单上传 |
| 递归下载网站 | wget | -r 递归下载内建支持 |
本篇小结
ping:测试连通性和延迟,ICMP Echo Request/Reply,关注 RTT 和丢包率curl:HTTP 请求瑞士军刀,-v调试神器,-dPOST 数据,-H自定义头wget:文件下载专用,-c断点续传,-b后台下载,进度显示友好curl适合 API 调试和复杂请求,wget适合简单文件下载
动手实践
ping 测试:
ping -c 4 baidu.com ping -c 4 8.8.8.8 # 对比 RTT 差异,为什么不同?curl 详细请求:
curl -v http://example.com # 观察 DNS 解析、TCP 连接、HTTP 请求头、响应头curl POST 请求:
curl -X POST -d "username=test&password=123" http://httpbin.org/postwget 下载测试:
wget -O test.html http://example.com cat test.html断点续传实验:
# 开始下载一个大文件,按 Ctrl+C 中断 wget http://speedtest.tele2.net/10MB.zip # 重新执行相同命令,观察是否续传 wget -c http://speedtest.tele2.net/10MB.zip思考:为什么
ping使用 ICMP 协议而不是 TCP 或 UDP?ICMP 有什么特点使其适合探测网络连通性?