Linux系统的ping
命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping
一下某机器,看是不是开着”、不能打开网页时会说“先ping
网关地址192.168.1.1
试试”。它通过发送ICMP ECHO_REQUEST数据包到网络主机(send ICMP ECHO_REQUEST to network hosts),并显示响应情况,这样就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的)。有些服务器为了防止通过ping
探测到,通过防火墙设置了禁止ping
或者在内核参数中禁止ping
,这样就不能通过ping
确定该主机是否还处于开启状态。
linux下的ping
命令和windows下的ping
执行稍有区别,linux下ping
不会自动终止,需要按ctrl+c
终止或者用参数-c
指定要求完成的回应次数。
ping [参数] [主机名或IP地址]
ping
命令用于:确定网络和各外部主机的状态;跟踪和隔离硬件和软件问题;测试、评估和管理网络。如果主机正在运行并连在网上,它就对回送信号进行响应。每个回送信号请求包含一个网际协议(IP)和 ICMP 头,后面紧跟一个 tim 结构,以及来填写这个信息包的足够的字节。缺省情况是连续发送回送信号请求直到接收到中断信号(Ctrl-C)。ping
命令每秒发送一个数据报并且为每个接收到的响应打印一行输出。ping
命令计算信号往返时间和(信息)包丢失情况的统计信息,并且在完成之后显示一个简要总结。ping
命令在程序超时或当接收到 SIGINT 信号时结束。Host
参数或者是一个有效的主机名或者是因特网地址。
-d
使用Socket的SO_DEBUG功能。-f
极限检测。大量且快速地送网络封包给一台机器,看它的回应。-n
只输出数值。-q
不显示任何传送封包的信息,只显示最后的结果。-r
忽略普通的Routing Table,直接将数据包送到远端主机上。通常是查看本机的网络接口是否有问题。-R
记录路由过程。-v
详细显示指令的执行过程。-c
数目:在发送指定数目的包后停止。-i
秒数:设定间隔几秒送一个网络封包给一台机器,预设值是一秒送一次。-I
网络界面:使用指定的网络界面送出数据包。-l
前置载入:设置在送出要求信息之前,先行发出的数据包。-p
范本样式:设置填满数据包的范本样式。-s
字节数:指定发送的数据字节数,预设值是56
,加上8字节的ICMP头,一共是64ICMP数据字节。-t
存活数值:设置存活数值TTL
的大小。命令:
ping 192.168.0.5
输出:
[zyiz@localhost ~]$ ping 192.168.0.5 PING 192.168.0.5 (192.168.0.5) 56(84) bytes of data. bytes from 192.168.0.5: icmp_seq=1 ttl=64 time=0.498 ms bytes from 192.168.0.5: icmp_seq=2 ttl=64 time=0.384 ms bytes from 192.168.0.5: icmp_seq=3 ttl=64 time=0.486 ms bytes from 192.168.0.5: icmp_seq=4 ttl=64 time=0.482 ms ^C --- 192.168.0.5 ping statistics --- packets transmitted, 4 received, 0% packet loss, time 3006ms rtt min/avg/max/mdev = 0.384/0.462/0.498/0.050 ms [zyiz@localhost ~]$
命令:
ping 192.168.0.202
输出:
[zyiz@localhost ~]$ ping 192.168.0.202 PING 192.168.0.202 (192.168.0.202) 56(84) bytes of data. From 192.168.0.197 icmp_seq=1 Destination Host Unreachable From 192.168.0.197 icmp_seq=2 Destination Host Unreachable From 192.168.0.197 icmp_seq=3 Destination Host Unreachable From 192.168.0.197 icmp_seq=4 Destination Host Unreachable From 192.168.0.197 icmp_seq=5 Destination Host Unreachable From 192.168.0.197 icmp_seq=6 Destination Host Unreachable ^C --- 192.168.0.202 ping statistics --- packets transmitted, 0 received, +6 errors, 100% packet loss, time 6023ms pipe 4 [zyiz@localhost ~]$
命令:
ping -b 192.168.0.0
输出:
[zyiz@localhost ~]$ route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 [zyiz@localhost ~]$ ping -b 192.168.0.0 WARNING: pinging broadcast address PING 192.168.0.0 (192.168.0.0) 56(84) bytes of data.
命令:
ping -c 10 www.baidu.com
输出:
[zyiz@localhost ~]$ ping -c 10 www.baidu.com PING www.baidu.com (14.215.177.38) 56(84) bytes of data. bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=56 time=11.8 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=56 time=12.4 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=56 time=13.3 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=56 time=12.2 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=56 time=11.7 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=56 time=12.8 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=7 ttl=56 time=17.6 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=8 ttl=56 time=12.7 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=9 ttl=56 time=12.2 ms bytes from 14.215.177.38 (14.215.177.38): icmp_seq=10 ttl=56 time=12.1 ms --- www.baidu.com ping statistics --- packets transmitted, 10 received, 0% packet loss, time 9078ms rtt min/avg/max/mdev = 11.782/12.942/17.696/1.644 ms [zyiz@localhost ~]$
说明: ping 百度(www.baidu.com) 10 次后自动退出。
命令:
ping -c 10 -i 0.5 www.zyiz.net
输出:
[zyiz@localhost ~]$ ping -c 10 -i 0.5 www.zyiz.net PING www.zyiz.net (112.124.103.85) 56(84) bytes of data. bytes from 112.124.103.85 (112.124.103.85): icmp_seq=1 ttl=55 time=45.7 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=2 ttl=55 time=45.6 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=3 ttl=55 time=46.1 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=4 ttl=55 time=45.4 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=5 ttl=55 time=45.8 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=6 ttl=55 time=46.1 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=7 ttl=55 time=46.1 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=8 ttl=55 time=45.3 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=9 ttl=55 time=45.3 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=10 ttl=55 time=45.5 ms --- www.zyiz.net ping statistics --- packets transmitted, 10 received, 0% packet loss, time 4531ms rtt min/avg/max/mdev = 45.301/45.729/46.187/0.339 ms [zyiz@localhost ~]$
说明: 每隔 0.5
秒的时间间隔,ping www.zyiz.net ,到达 10
次数后退出。
命令:
ping -c 5 www.zyiz.net
输出:
[zyiz@localhost ~]$ ping -c 5 www.zyiz.net PING www.zyiz.net (112.124.103.85) 56(84) bytes of data. bytes from 112.124.103.85 (112.124.103.85): icmp_seq=1 ttl=55 time=45.6 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=2 ttl=55 time=45.9 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=3 ttl=55 time=46.1 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=4 ttl=55 time=45.3 ms bytes from 112.124.103.85 (112.124.103.85): icmp_seq=5 ttl=55 time=46.1 ms --- www.zyiz.net ping statistics --- packets transmitted, 5 received, 0% packet loss, time 4017ms rtt min/avg/max/mdev = 45.332/45.848/46.166/0.413 ms [zyiz@localhost ~]$ ping -c 5 www.studyoracle.cn PING www.studyoracle.cn (123.57.223.216) 56(84) bytes of data. bytes from 123.57.223.216 (123.57.223.216): icmp_seq=1 ttl=53 time=54.3 ms bytes from 123.57.223.216 (123.57.223.216): icmp_seq=2 ttl=53 time=54.1 ms bytes from 123.57.223.216 (123.57.223.216): icmp_seq=3 ttl=53 time=53.9 ms bytes from 123.57.223.216 (123.57.223.216): icmp_seq=4 ttl=53 time=54.4 ms bytes from 123.57.223.216 (123.57.223.216): icmp_seq=5 ttl=53 time=54.0 ms --- www.studyoracle.cn ping statistics --- packets transmitted, 5 received, 0% packet loss, time 4034ms rtt min/avg/max/mdev = 53.981/54.184/54.438/0.232 ms [zyiz@localhost ~]$
命令:
ping -i 3 -s 1024 -t 255 192.168.0.5
输出:
[zyiz@localhost ~]$ ping -i 3 -s 1024 -t 255 192.168.0.5 PING 192.168.0.5 (192.168.0.5) 1024(1052) bytes of data. bytes from 192.168.0.5: icmp_seq=1 ttl=64 time=0.644 ms bytes from 192.168.0.5: icmp_seq=2 ttl=64 time=1.36 ms bytes from 192.168.0.5: icmp_seq=3 ttl=64 time=0.377 ms bytes from 192.168.0.5: icmp_seq=4 ttl=64 time=1.45 ms bytes from 192.168.0.5: icmp_seq=5 ttl=64 time=1.46 ms bytes from 192.168.0.5: icmp_seq=6 ttl=64 time=1.45 ms ^C --- 192.168.0.5 ping statistics --- packets transmitted, 6 received, 0% packet loss, time 15038ms rtt min/avg/max/mdev = 0.377/1.125/1.468/0.444 ms [zyiz@localhost ~]$
说明:-i 3
发送周期为 3
秒, -s
设置发送包的大小为1024
, -t
设置TTL值为 255
。