Linux下ping命令、traceroute命令、tracert命令的使用


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 詳細顯示指令的執行過程。

<p>-c 數目:在發送指定數目的包后停止。

-i 秒數:設定間隔幾秒送一個網絡封包給一台機器,預設值是一秒送一次。

-I 網絡界面:使用指定的網絡界面送出數據包。

-l 前置載入:設置在送出要求信息之前,先行發出的數據包。

-p 范本樣式:設置填滿數據包的范本樣式。

-s 字節數:指定發送的數據字節數,預設值是56,加上8字節的ICMP頭,一共是64ICMP數據字節。

-t 存活數值:設置存活數值TTL的大小。

4.使用實例:

實例1:ping的通的情況

命令:

ping 192.168.120.205

輸出:

復制代碼
[root@localhost ~]# ping 192.168.120.205
PING 192.168.120.205 (192.168.120.205) 56(84) bytes of data.
64 bytes from 192.168.120.205: icmp_seq=1 ttl=64 time=0.720 ms
64 bytes from 192.168.120.205: icmp_seq=2 ttl=64 time=0.181 ms
64 bytes from 192.168.120.205: icmp_seq=3 ttl=64 time=0.191 ms
64 bytes from 192.168.120.205: icmp_seq=4 ttl=64 time=0.188 ms
64 bytes from 192.168.120.205: icmp_seq=5 ttl=64 time=0.189 ms

--- 192.168.120.205 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 0.181/0.293/0.720/0.214 ms
[root@localhost ~]# 
復制代碼

說明:

 

實例2:ping不通的情況

命令:

ping 192.168.120.202

輸出:

復制代碼
[root@localhost ~]# ping 192.168.120.202
PING 192.168.120.202 (192.168.120.202) 56(84) bytes of data.
From 192.168.120.204 icmp_seq=1 Destination Host Unreachable
From 192.168.120.204 icmp_seq=2 Destination Host Unreachable
From 192.168.120.204 icmp_seq=3 Destination Host Unreachable
From 192.168.120.204 icmp_seq=4 Destination Host Unreachable
From 192.168.120.204 icmp_seq=5 Destination Host Unreachable
From 192.168.120.204 icmp_seq=6 Destination Host Unreachable

--- 192.168.120.202 ping statistics ---
8 packets transmitted, 0 received, +6 errors, 100% packet loss, time 7005ms
, pipe 4
[root@localhost ~]#
復制代碼

說明:

 

實例3:ping網關

命令:

ping -b 192.168.120.1

輸出:

復制代碼
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# ping -b 192.168.120.1
PING 192.168.120.1 (192.168.120.1) 56(84) bytes of data.
64 bytes from 192.168.120.1: icmp_seq=1 ttl=255 time=2.02 ms
64 bytes from 192.168.120.1: icmp_seq=2 ttl=255 time=1.83 ms
64 bytes from 192.168.120.1: icmp_seq=3 ttl=255 time=1.68 ms
64 bytes from 192.168.120.1: icmp_seq=4 ttl=255 time=1.98 ms
64 bytes from 192.168.120.1: icmp_seq=5 ttl=255 time=1.88 ms

--- 192.168.120.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 1.682/1.880/2.020/0.129 ms
復制代碼

說明:

 

實例4:ping指定次數

命令:

ping -c 10 192.168.120.206

輸出:

復制代碼
[root@localhost ~]# ping -c 10 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.
64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.25 ms
64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.260 ms
64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.242 ms
64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.271 ms
64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.274 ms
64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.295 ms
64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.269 ms
64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.270 ms
64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.253 ms
64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.289 ms

--- 192.168.120.206 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9000ms
rtt min/avg/max/mdev = 0.242/0.367/1.251/0.295 ms
[root@localhost ~]# 
復制代碼

說明:

 

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

命令:

ping -c 10 -i 0.5 192.168.120.206

輸出:

復制代碼
[root@localhost ~]# ping -c 10 -i 0.5 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.
64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.24 ms
64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.235 ms
64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.244 ms
64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.300 ms
64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.255 ms
64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.264 ms
64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.263 ms
64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.331 ms
64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.247 ms
64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.244 ms

--- 192.168.120.206 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 4499ms
rtt min/avg/max/mdev = 0.235/0.362/1.241/0.294 ms
[root@localhost ~]# ping -c 10 -i 0.01 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.
64 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=0.244 ms
64 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.195 ms
64 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.219 ms
64 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.204 ms
64 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=3.56 ms
64 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=1.93 ms
64 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.193 ms
64 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.193 ms
64 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.202 ms
64 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.211 ms

--- 192.168.120.206 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 90ms
rtt min/avg/max/mdev = 0.193/0.716/3.564/1.080 ms
[root@localhost ~]# 
復制代碼

 

說明:

 

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

命令:

ping -c 5 www.58.com

輸出:

復制代碼
peida-VirtualBox ~ # ping -c 5 www.58.com
PING www.58.com (211.151.111.30) 56(84) bytes of data.
64 bytes from 211.151.111.30: icmp_req=1 ttl=49 time=14.7 ms
64 bytes from 211.151.111.30: icmp_req=2 ttl=49 time=16.4 ms
64 bytes from 211.151.111.30: icmp_req=3 ttl=49 time=15.2 ms
64 bytes from 211.151.111.30: icmp_req=4 ttl=49 time=14.6 ms
64 bytes from 211.151.111.30: icmp_req=5 ttl=49 time=19.9 ms

--- www.58.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 20101ms
rtt min/avg/max/mdev = 14.618/16.192/19.917/1.965 ms
peida-VirtualBox ~ # 
復制代碼

說明:

 

實例7:多參數使用

命令:

ping -i 3 -s 1024 -t 255 192.168.120.206

輸出:

復制代碼
[root@localhost ~]# ping -i 3 -s 1024 -t 255 192.168.120.206
PING 192.168.120.206 (192.168.120.206) 1024(1052) bytes of data.
1032 bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.99 ms
1032 bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.694 ms
1032 bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.300 ms
1032 bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.481 ms
1032 bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.415 ms
1032 bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.600 ms
1032 bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.411 ms
1032 bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.281 ms
1032 bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.318 ms
1032 bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.362 ms
1032 bytes from 192.168.120.206: icmp_seq=11 ttl=64 time=0.408 ms
1032 bytes from 192.168.120.206: icmp_seq=12 ttl=64 time=0.445 ms
1032 bytes from 192.168.120.206: icmp_seq=13 ttl=64 time=0.397 ms
1032 bytes from 192.168.120.206: icmp_seq=14 ttl=64 time=0.406 ms
1032 bytes from 192.168.120.206: icmp_seq=15 ttl=64 time=0.458 ms

--- 192.168.120.206 ping statistics ---
15 packets transmitted, 15 received, 0% packet loss, time 41999ms
rtt min/avg/max/mdev = 0.281/0.531/1.993/0.404 ms
[root@localhost ~]# 
復制代碼

說明:

-i 3 發送周期為 3秒 -s 設置發送包的大小為1024 -t 設置TTL值為 255

 

 

 

 

 

 

在大多數情況下,作為網絡工程技術人員或者系統管理員會在UNIX主機系統下,直接執行命令行: 
  Traceroute hostname 
而在Windows系統下是執行Tracert的命令: 
Tracert hostname 
比如在北京地區使用windows NT 主機(已經與北京163建立了點對點的連接后) 
使用NT系統中的Tracert命令:(用戶可用:開始->運行,輸入"command" 調出command窗口使用此命令)

C:\>tracert www.yahoo.com

參數說明:

tracert [-d] [-h maximum_hops] [-j computer-list] [-w timeout] target_name

參數 
-d 
指定不對計算機名解析地址。 
-h maximum_hops 
指定查找目標的跳轉的最大數目。 
-jcomputer-list 
指定在 computer-list 中松散源路由。 
-w timeout 
等待由 timeout 對每個應答指定的毫秒數。 
target_name 
目標計算機的名稱。

Traceroute的命令參數:

Traceroute的用法為: Traceroute [options] <IP-address or domain-name> [data size]

[options]的內容有:

[-n]:顯示的地址是用數字表示而不是符號

[-v]:長輸出

[-p]:UDP端口設置(缺省為33434)

[-q]:設置TTL測試數目(缺省為3)

[-t]:設置測包的服務類型

[data size]:每次測試包的數據字節長度(缺省為38)

Traceroute的工作原理: 
Traceroute最簡單的基本用法是:traceroute hostname

Traceroute程序的設計是利用ICMP及IP header的TTL(Time To Live)欄位(field)。首先,traceroute送出一個TTL是1的IP datagram(其實,每次送出的為3個40字節的包,包括源地址,目的地址和包發出的時間標簽)到目的地,當路徑上的第一個路由器(router)收到這個datagram時,它將TTL減1。此時,TTL變為0了,所以該路由器會將此datagram丟掉,並送回一個「ICMP time exceeded」消息(包括發IP包的源地址,IP包的所有內容及路由器的IP地址),traceroute 收到這個消息后,便知道這個路由器存在於這個路徑上,接着traceroute 再送出另一個TTL是2 的datagram,發現第2 個路由器...... traceroute 每次將送出的datagram的TTL 加1來發現另一個路由器,這個重復的動作一直持續到某個datagram 抵達目的地。當datagram到達目的地后,該主機並不會送回ICMP time exceeded消息,因為它已是目的地了,那么traceroute如何得知目的地到達了呢?

Traceroute在送出UDP datagrams到目的地時,它所選擇送達的port number 是一個一般應用程序都不會用的號碼(30000 以上),所以當此UDP datagram 到達目的地后該主機會送回一個「ICMP port unreachable」的消息,而當traceroute 收到這個消息時,便知道目的地已經到達了。所以traceroute 在Server端也是沒有所謂的Daemon 程式。

Traceroute提取發 ICMP TTL到期消息設備的IP地址並作域名解析。每次 ,Traceroute都打印出一系列數據,包括所經過的路由設備的域名及 IP地址,三個包每次來回所花時間。

Traceroute 有一個固定的時間等待響應(ICMP TTL到期消息)。如果這個時間過了,它將打印出一系列的*號表明:在這個路徑上,這個設備不能在給定的時間內發出ICMP TTL到期消息的響應。然后,Traceroute給TTL記數器加1,繼續進行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM