ping 是 Linux 中用于测试网络连通性和测量网络延迟的命令。它通过发送 ICMP 回显请求包来测试目标主机的可达性。
1 2 3 4 5
| ➜ ping --help 用法: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface] [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] [hop ...] destination
|
基本语法
常用选项
| 选项 |
说明 |
-c, --count=N |
发送 N 个包后停止 |
-i, --interval=N |
发送间隔(秒) |
-s, --size=N |
数据包大小(字节) |
-t, --ttl=N |
设置 TTL 值 |
-W, --timeout=N |
等待响应的超时时间(秒) |
-q, --quiet |
安静模式,只显示统计信息 |
-v, --verbose |
详细输出 |
-f, --flood |
洪水模式(快速发送,需 root) |
-I, --interface=IFACE |
指定网络接口 |
基本使用
基本 ping
1 2 3 4 5 6 7 8
| ➜ ping example.com
➜ ping -c 4 example.com
➜ ping 8.8.8.8
|
设置间隔
1 2 3 4 5
| ➜ ping -i 2 example.com
➜ ping -i 0.2 example.com
|
设置数据包大小
1 2
| ➜ ping -s 1024 example.com
|
设置超时
1 2
| ➜ ping -W 5 example.com
|
安静模式
1 2
| ➜ ping -q -c 10 example.com
|
实际应用场景
网络连通性测试
1 2 3 4 5 6 7 8
| ➜ ping 192.168.1.1
➜ ping 8.8.8.8
➜ ping example.com
|
网络质量测试
1 2 3 4 5 6
| ➜ ping -c 100 example.com
|
持续监控
1 2 3 4 5 6 7 8 9 10
| ➜ ping -i 1 example.com
while ping -c 1 example.com > /dev/null; do echo "Network is up" sleep 5 done echo "Network is down"
|
注意事项
- 权限:某些选项(如
-f)需要 root 权限
- 防火墙:某些主机可能禁 ping(ICMP)
- 频率限制:不要过于频繁地 ping,避免被视为攻击
参考文献