5.2 ping
ping 命令是我们最常用的命令之一,一般用于侦测本机到目标主机的网络是否通畅。其使用方法是:
ping ip地址
1
使用方法如下:
[root@localhost ~]# ping 120.55.94.78
PING 120.55.94.78 (120.55.94.78) 56(84) bytes of data.
64 bytes from 120.55.94.78: icmp_seq=1 ttl=128 time=11.0 ms
64 bytes from 120.55.94.78: icmp_seq=2 ttl=128 time=17.3 ms
64 bytes from 120.55.94.78: icmp_seq=3 ttl=128 time=16.2 ms
64 bytes from 120.55.94.78: icmp_seq=4 ttl=128 time=10.6 ms
64 bytes from 120.55.94.78: icmp_seq=5 ttl=128 time=10.2 ms
64 bytes from 120.55.94.78: icmp_seq=6 ttl=128 time=18.7 ms
64 bytes from 120.55.94.78: icmp_seq=7 ttl=128 time=15.8 ms
64 bytes from 120.55.94.78: icmp_seq=8 ttl=128 time=10.8 ms
64 bytes from 120.55.94.78: icmp_seq=9 ttl=128 time=10.8 ms
64 bytes from 120.55.94.78: icmp_seq=10 ttl=128 time=11.5 ms
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
上面的输出为目标网络可达的情形。在 Linux 操作系统上,如果目标网络不可达,ping 命令会一直发送而无输出结果,直到我们按下 Ctrl + C 中断下来才会有统计结果,效果如下:
[root@localhost ~]# ping 120.55.94.79
PING 120.55.94.79 (120.55.94.79) 56(84) bytes of data.
^C^C
--- 120.55.94.79 ping statistics ---
578 packets transmitted, 0 received, 100% packet loss, time 577119ms
1
2
3
4
5
6
2
3
4
5
6
Windows 系统上,这种情况下,ping 命令会返回超时的包数量,而不是一直阻塞。且在 Linux 系统上 ping 命令是一直发送数据包,直到人工主动中断;而 Windows 机器上默认会发送 4 个数据包后停止,如果想一直发送而不停止,可以使用 -t 选项。
当然,ping 命令的目标也可以是一个域名,这样通过 ping 这个域名,我们可以得到这个域名解析后的 ip 地址:
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=128 time=30.3 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=128 time=28.8 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=128 time=29.0 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=128 time=31.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=128 time=28.8 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=6 ttl=128 time=27.8 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=7 ttl=128 time=29.0 ms
^C
--- www.a.shifen.com ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6011ms
rtt min/avg/max/mdev = 27.822/29.430/31.968/1.244 ms
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
我们这里得到 www.baidu.com 对应的 ip 地址是 61.135.169.121。
ping 命令是通过发送 ICMP 数据包来实现的。
上次更新: 2024/07/08, 00:14:14