在linux系統(tǒng)里面如果想判斷網(wǎng)絡(luò)的好壞,詳細(xì)腦海中蹦出的第一個(gè)命令就是ping了。ping命令基本是最常用的網(wǎng)絡(luò)命令,它可以用來(lái)測(cè)試與目標(biāo)主機(jī)的連通性。ping通過(guò)發(fā)送ICMP ECHO_REQUEST數(shù)據(jù)包到網(wǎng)絡(luò)主機(jī),并顯示返回的相應(yīng)情況,根據(jù)這些信息就可以判斷目標(biāo)主機(jī)是否可以訪(fǎng)問(wèn)。不過(guò)有些服務(wù)器為了防止通過(guò)ping探測(cè)到,可能會(huì)在防火墻或者內(nèi)核參數(shù)中禁止ping命令,這樣的話(huà),可能雖然目標(biāo)主機(jī)可以訪(fǎng)問(wèn),但是無(wú)法ping通。需要注意linux下的ping和windows下的ping稍有區(qū)別,linux下ping不會(huì)自動(dòng)終止,需要按ctrl+c終止或者用參數(shù)-c指定要求完成的回應(yīng)次數(shù)。
命令格式:ping [參數(shù)] [主機(jī)名或者IP地址]。使用范例,不加任何參數(shù),如果不加任何參數(shù),查看是否ping通。
~ ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_seq=1 ttl=52 time=6.06 ms
64 bytes from 115.239.210.27: icmp_seq=2 ttl=52 time=5.56 ms
64 bytes from 115.239.210.27: icmp_seq=3 ttl=52 time=5.67 ms
64 bytes from 115.239.210.27: icmp_seq=4 ttl=52 time=5.82 ms
64 bytes from 115.239.210.27: icmp_seq=5 ttl=52 time=5.70 ms
64 bytes from 115.239.210.27: icmp_seq=6 ttl=52 time=5.79 ms
--- 192.168.254.44 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.152/0.159/0.172/0.017 ms
發(fā)送指定數(shù)目,可以通過(guò) 參數(shù)-c 來(lái)發(fā)送指定數(shù)目的包后停止.
~ ping www.baidu.com -c 5
PING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.
64 bytes from 115.239.211.112: icmp_seq=1 ttl=52 time=6.03 ms
64 bytes from 115.239.211.112: icmp_seq=2 ttl=52 time=5.96 ms
64 bytes from 115.239.211.112: icmp_seq=3 ttl=52 time=5.79 ms
64 bytes from 115.239.211.112: icmp_seq=4 ttl=52 time=5.79 ms
64 bytes from 115.239.211.112: icmp_seq=5 ttl=52 time=6.21 ms
--- www.a.shifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 5.791/5.958/6.215/0.186 ms
設(shè)定發(fā)送間隔
可以通過(guò) 參數(shù) -c 數(shù)目:在發(fā)送指定數(shù)目的包后停止
~ ping www.baidu.com -c 5 -i 2
PING www.a.shifen.com (115.239.211.112) 56(84) bytes of data.
64 bytes from 115.239.211.112: icmp_seq=1 ttl=52 time=6.06 ms
64 bytes from 115.239.211.112: icmp_seq=2 ttl=52 time=5.84 ms
64 bytes from 115.239.211.112: icmp_seq=3 ttl=52 time=5.98 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 5.842/5.965/6.068/0.112 ms