ping命令

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指定要求完成的迴應次數。

1.命令格式

ping [參數] [主機名或IP地址]

2.命令功能

ping命令用於:確定網絡和各外部主機的狀態;跟蹤和隔離硬件和軟件問題;測試、評估和管理網絡。如果主機正在運行並連在網上,它就對回送信號進行響應。每個回送信號請求包含一個網際協議(IP)和 ICMP 頭,後面緊跟一個 tim 結構,以及來填寫這個信息包的足夠的字節。缺省情況是連續發送回送信號請求直到接收到中斷信號(Ctrl-C)。
ping 命令每秒發送一個數據報並且爲每個接收到的響應打印一行輸出。ping 命令計算信號往返時間和(信息)包丟失情況的統計信息,並且在完成之後顯示一個簡要總結。ping 命令在程序超時或當接收到 SIGINT 信號時結束。Host 參數或者是一個有效的主機名或者是因特網地址。

3.命令參數

  • -d 使用Socket的SO_DEBUG功能。
  • -f 極限檢測。大量且快速地送網絡封包給一臺機器,看它的迴應。
  • -n 只輸出數值。
  • -q 不顯示任何傳送封包的信息,只顯示最後的結果。
  • -r 忽略普通的Routing Table,直接將數據包送到遠端主機上。通常是查看本機的網絡接口是否有問題。
  • -R 記錄路由過程。
  • -v 詳細顯示指令的執行過程。
  • -c 數目:在發送指定數目的包後停止。
  • -i 秒數:設定間隔幾秒送一個網絡封包給一臺機器,預設值是一秒送一次。
  • -I 網絡界面:使用指定的網絡界面送出數據包。
  • -l 前置載入:設置在送出要求信息之前,先行發出的數據包。
  • -p 範本樣式:設置填滿數據包的範本樣式。
  • -s 字節數:指定發送的數據字節數,預設值是56,加上8字節的ICMP頭,一共是64ICMP數據字節。
  • -t 存活數值:設置存活數值TTL的大小。

4.使用實例

實例1:ping的通的情況

命令:

ping 192.168.0.5

輸出:

[yiibai@localhost ~]$ ping 192.168.0.5
PING 192.168.0.5 (192.168.0.5) 56(84) bytes of data.
64 bytes from 192.168.0.5: icmp_seq=1 ttl=64 time=0.498 ms
64 bytes from 192.168.0.5: icmp_seq=2 ttl=64 time=0.384 ms
64 bytes from 192.168.0.5: icmp_seq=3 ttl=64 time=0.486 ms
64 bytes from 192.168.0.5: icmp_seq=4 ttl=64 time=0.482 ms
^C
--- 192.168.0.5 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 0.384/0.462/0.498/0.050 ms
[yiibai@localhost ~]$

實例2:ping不通的情況

命令:

ping 192.168.0.202

輸出:

[yiibai@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 ---
7 packets transmitted, 0 received, +6 errors, 100% packet loss, time 6023ms
pipe 4
[yiibai@localhost ~]$

實例3:ping網關

命令:

ping -b 192.168.0.0

輸出:

[yiibai@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
[yiibai@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.

實例4:ping指定次數

命令:

ping -c 10 www.baidu.com

輸出:

[yiibai@localhost ~]$ ping -c 10 www.baidu.com
PING www.baidu.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=1 ttl=56 time=11.8 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=2 ttl=56 time=12.4 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=3 ttl=56 time=13.3 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=4 ttl=56 time=12.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=5 ttl=56 time=11.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=6 ttl=56 time=12.8 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=7 ttl=56 time=17.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=8 ttl=56 time=12.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=9 ttl=56 time=12.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=10 ttl=56 time=12.1 ms

--- www.baidu.com ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9078ms
rtt min/avg/max/mdev = 11.782/12.942/17.696/1.644 ms
[yiibai@localhost ~]$

說明: ping 百度(www.baidu.com) 10 次後自動退出。

實例5:時間間隔和次數限制的ping

命令:

ping -c 10 -i 0.5 www.yiibai.com

輸出:

[yiibai@localhost ~]$ ping -c 10 -i 0.5 www.yiibai.com
PING www.yiibai.com (112.124.103.85) 56(84) bytes of data.
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=1 ttl=55 time=45.7 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=2 ttl=55 time=45.6 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=3 ttl=55 time=46.1 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=4 ttl=55 time=45.4 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=5 ttl=55 time=45.8 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=6 ttl=55 time=46.1 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=7 ttl=55 time=46.1 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=8 ttl=55 time=45.3 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=9 ttl=55 time=45.3 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=10 ttl=55 time=45.5 ms

--- www.yiibai.com ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 4531ms
rtt min/avg/max/mdev = 45.301/45.729/46.187/0.339 ms
[yiibai@localhost ~]$

說明: 每隔 0.5 秒的時間間隔,ping www.yiibai.com ,到達 10 次數後退出。

實例6:通過域名ping公網上的站點

命令:

ping -c 5 www.yiibai.com

輸出:

[yiibai@localhost ~]$ ping -c 5 www.yiibai.com
PING www.yiibai.com (112.124.103.85) 56(84) bytes of data.
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=1 ttl=55 time=45.6 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=2 ttl=55 time=45.9 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=3 ttl=55 time=46.1 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=4 ttl=55 time=45.3 ms
64 bytes from 112.124.103.85 (112.124.103.85): icmp_seq=5 ttl=55 time=46.1 ms

--- www.yiibai.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4017ms
rtt min/avg/max/mdev = 45.332/45.848/46.166/0.413 ms
[yiibai@localhost ~]$ ping -c 5 www.studyoracle.cn
PING www.studyoracle.cn (123.57.223.216) 56(84) bytes of data.
64 bytes from 123.57.223.216 (123.57.223.216): icmp_seq=1 ttl=53 time=54.3 ms
64 bytes from 123.57.223.216 (123.57.223.216): icmp_seq=2 ttl=53 time=54.1 ms
64 bytes from 123.57.223.216 (123.57.223.216): icmp_seq=3 ttl=53 time=53.9 ms
64 bytes from 123.57.223.216 (123.57.223.216): icmp_seq=4 ttl=53 time=54.4 ms
64 bytes from 123.57.223.216 (123.57.223.216): icmp_seq=5 ttl=53 time=54.0 ms

--- www.studyoracle.cn ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4034ms
rtt min/avg/max/mdev = 53.981/54.184/54.438/0.232 ms
[yiibai@localhost ~]$

實例7:多參數使用

命令:

ping -i 3 -s 1024 -t 255 192.168.0.5

輸出:

[yiibai@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.
1032 bytes from 192.168.0.5: icmp_seq=1 ttl=64 time=0.644 ms
1032 bytes from 192.168.0.5: icmp_seq=2 ttl=64 time=1.36 ms
1032 bytes from 192.168.0.5: icmp_seq=3 ttl=64 time=0.377 ms
1032 bytes from 192.168.0.5: icmp_seq=4 ttl=64 time=1.45 ms
1032 bytes from 192.168.0.5: icmp_seq=5 ttl=64 time=1.46 ms
1032 bytes from 192.168.0.5: icmp_seq=6 ttl=64 time=1.45 ms
^C
--- 192.168.0.5 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 15038ms
rtt min/avg/max/mdev = 0.377/1.125/1.468/0.444 ms
[yiibai@localhost ~]$

說明:-i 3 發送週期爲 3 秒, -s 設置發送包的大小爲1024-t 設置TTL值爲 255