這篇文章通過(guò)一個(gè)實(shí)例,演示ping、traceroute、debug等命令的用法;測試網(wǎng)絡(luò )連通性,分析網(wǎng)絡(luò )數據不可達的原因。
___________________________________________
文章目錄
___________________________________________
[*1*].實(shí)例拓撲圖與配置
C1是使用VPCS虛擬出來(lái)的一臺計算機,SW1是一臺不可網(wǎng)管交換機,R1、R2、R3上都添加了”NM-4T”串口,R1上還添加了一個(gè)”NM-1FE-TX”快速以太網(wǎng)端口。R3上面配置了一個(gè)回環(huán)接口3.3.3.3/24,各設備IP配置和連接如下圖:
R1配置如下:
5 | R1(config-line)#logg syn |
6 | R1(config-line)#exec-time 0 0 |
9 | R1(config-if)#ip add 192.168.1.1 255.255.255.0 |
11 | R1(config-if)#int s 0/0 |
12 | R1(config-if)#ip add 12.1.1.1 255.255.255.0 |
R2配置如下:
3 | Router(config)#line co 0 |
4 | Router(config-line)#logg syn |
5 | Router(config-line)#exec-time 0 0 |
6 | Router(config-line)#exit |
9 | R2(config-if)#ip add 12.1.1.2 255.255.255.0 |
11 | R2(config-if)#int s 0/2 |
12 | R2(config-if)#ip add 23.1.1.2 255.255.255.0 |
R3配置如下:
5 | R3(config-line)#logg syn |
6 | R3(config-line)#exec-time 0 0 |
7 | R3(config-line)#int s 0/3 |
8 | R3(config-if)#ip add 23.1.1.3 255.255.255.0 |
11 | R3(config)#int loopback 0 /*配置回環(huán)接口0*/ |
12 | R3(config-if)#ip add 3.3.3.3 255.255.255.0 |
[*2*].測試連通性
首先在C1上面測試ping自己的網(wǎng)關(guān)(R1的fa1/0接口)
1 | VPCS[1]> ping 192.168.1.1 |
2 | 192.168.1.1 icmp_seq=1 ttl=255 time=49.000 ms /*可以ping通*/ |
4 | /*繼續在C1上測試ping不同網(wǎng)段的地址*/ |
7 | 12.1.1.1 icmp_seq=1 ttl=255 time=29.000 ms /*ping網(wǎng)關(guān)上的串口,成功*/ |
9 | VPCS[1]> ping 12.1.1.2 /*ping R2和R1相連的接口,失敗,超時(shí)*/ |
10 | 12.1.1.2 icmp_seq=1 timeout |
這一步為什么會(huì )超時(shí)呢?是不是數據包沒(méi)有到達R2?我們在R2上開(kāi)啟debug命令進(jìn)行調試:
2 | ICMP packet debugging is on |
4 | R2# /*開(kāi)啟調試后,再一次用C1去ping 12.1.1.2,發(fā)現R2上出現了下面的提示*/ |
5 | *Mar 1 00:17:10.135: ICMP: echo reply sent, src 12.1.1.2, dst 192.168.1.2 |
7 | /*關(guān)閉所有調試的方法是undebug all*/ |
從上面的顯示可以看出R2收到了C1發(fā)送過(guò)來(lái)的ICMP包,我們查看一下R2的路由表:
5 | Gateway of last resort is not set |
7 | 23.0.0.0/24 is subnetted, 1 subnets |
8 | C 23.1.1.0 is directly connected, Serial0/2 |
9 | 12.0.0.0/24 is subnetted, 1 subnets |
10 | C 12.1.1.0 is directly connected, Serial0/1 |
15 | * 從輸出可以明顯的看到R2的路由表中有兩個(gè)直連條目(C) |
16 | * 發(fā)往23.1.1.0/24網(wǎng)段的數據從Serial0/2發(fā)出 |
17 | * 發(fā)往12.1.1.0/24網(wǎng)段的數據從Serial0/1發(fā)出 |
這樣就很清楚的發(fā)現,并沒(méi)有說(shuō)明發(fā)往192.168.1.0/24網(wǎng)段的數據應該怎么發(fā)送,所以R2丟棄發(fā)往192.168.1.2發(fā)送過(guò)來(lái)的數據。
下面給R2添加靜態(tài)路由,讓他知道發(fā)往192.168.1.0/24網(wǎng)段的數據應該發(fā)給12.1.1.1(R1),這樣,C1再ping 12.1.1.2,就能ping通了:
1 | R2(config)#ip route 192.168.1.0 255.255.255.0 12.1.1.1 |
這個(gè)時(shí)候實(shí)際上C1 ping R3的任何地址都是ping不通的,因為數據包到達R1后,R1檢查自己的路由表,它會(huì )發(fā)現沒(méi)有任何去往R3的路由條目(3.3.3.0/24、23.1.1.0/24),所以它會(huì )直接給C1回復一個(gè)主機不可達:
2 | *192.168.1.1 icmp_seq=1 ttl=255 time=20.000 ms (ICMP type:3, code:1, Destination host unreachable) |
5 | *192.168.1.1 icmp_seq=1 ttl=255 time=20.000 ms (ICMP type:3, code:1, Destination host unreachable) |
8 | *192.168.1.1 icmp_seq=1 ttl=255 time=26.000 ms (ICMP type:3, code:1, Destination host unreachable) |
在R1上開(kāi)啟ICMP調試就能看到
2 | ICMP packet debugging is on |
4 | *Mar 1 00:34:55.587: ICMP: dst (23.1.1.3) host unreachable sent to 192.168.1.2 |
要想讓這個(gè)拓撲圖中的各個(gè)設備都能互相ping通,需要在R1、R2、R3上面添加下面的靜態(tài)路由條目:
1 | /*R1添加一條默認路由目的地是12.1.1.2(R2)*/ |
2 | R1(config)#ip route 0.0.0.0 0.0.0.0 12.1.1.2 |
5 | R2(config)#ip route 192.168.1.0 255.255.255.0 12.1.1.1 |
6 | R2(config)#ip route 3.3.3.0 255.255.255.0 23.1.1.3 |
9 | R3(config)#ip route 0.0.0.0 0.0.0.0 23.1.1.2 |
這個(gè)時(shí)候,不論在哪個(gè)設備上,都能ping通拓撲上的所有接口IP。下面是R1上面ping R3回環(huán)接口的返回信息:
3 | Type escape sequence to abort. |
4 | Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds: |
8 | * 這里顯示五個(gè)感嘆號,說(shuō)明上面默認ping發(fā)送5次,ping成功5次 |
9 | * 如果超時(shí)會(huì )顯示省略號"....." |
關(guān)閉R3的回環(huán)接口,就會(huì )出現超時(shí)的情況:
3 | R3(config-if)#shut /*關(guān)閉R3的回環(huán)接口*/ |
6 | /*使用R1去ping,顯示超時(shí),實(shí)際上這里產(chǎn)生了路由環(huán)路*/ |
9 | Type escape sequence to abort. |
10 | Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds: |
12 | *Mar 1 00:45:09.355: ICMP: time exceeded rcvd from 23.1.1.2. |
15 | *Mar 1 00:45:07.991: ICMP: time exceeded (time to live) sent to 12.1.1.1 (dest was 3.3.3.3) |
實(shí)際上這里就是因為R3關(guān)閉了換回接口,R1首先根據自己的默認路由將數據發(fā)送給R2,R2再根據自己的靜態(tài)路由將數據發(fā)送給R3,如果回環(huán)口沒(méi)有關(guān)閉,R3將應答這個(gè)ICMP,但是此時(shí)R3上回環(huán)關(guān)閉了,R3根據自己的默認路由又將數據發(fā)回R2,R2再次將數據發(fā)回R3,這樣直到IP報頭里面的TTL字段減小到0,丟棄這個(gè)數據。
下面我們打開(kāi)R3的lo0回環(huán)接口,關(guān)閉R2的s0/2,再次用R1去ping 3.3.3.3
1 | /*關(guān)閉R2與R3相連的s0/2接口*/ |
8 | Type escape sequence to abort. |
9 | Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds: |
13 | *Mar 1 00:53:32.091: ICMP: dst (12.1.1.1) host unreachable rcv from 12.1.1.2 |
17 | *Mar 1 00:53:34.823: ICMP: dst (3.3.3.3) host unreachable sent to 12.1.1.1 |
20 | * 查看R2路由表,發(fā)現直連接口條目23.1.1.0/24消失了 |
21 | * 同樣,目的地址是這個(gè)直連接口網(wǎng)段的靜態(tài)路由條目也消失了 |
22 | * 所以R2返回一個(gè)消息告訴R1"(3.3.3.3) host unreachable" |
26 | Gateway of last resort is not set |
28 | 12.0.0.0/24 is subnetted, 1 subnets |
29 | C 12.1.1.0 is directly connected, Serial0/1 |
30 | S 192.168.1.0/24 [1/0] via 12.1.1.1 |
打開(kāi)R2的s0/2繼續下面的實(shí)驗。
[*3*].高級ping命令和路由追蹤命令traceroute
使用高級ping命令,R1 ping R3的回環(huán)接口:
1 | R1#ping /*直接輸入ping,回車(chē)*/ |
2 | Protocol [ip]: /*選擇協(xié)議,默認回車(chē)即可*/ |
3 | Target IP address: 3.3.3.3 /*選擇目標IP,這里是R3的lo0接口IP*/ |
4 | Repeat count [5]: 10 /*ping次數,這里輸入了10次,默認5次*/ |
5 | Datagram size [100]: /*數據包大小,默認回車(chē)即可*/ |
6 | Timeout in seconds [2]: /*超時(shí)時(shí)間,直接回車(chē)*/ |
7 | Extended commands [n]: y /*是否顯示擴展命令,輸入y*/ |
8 | Source address or interface: 192.168.1.1 /*選擇用本地的哪個(gè)接口去ping,這里選擇R1的以太網(wǎng)接口*/ |
9 | Type of service [0]: /*下面暫時(shí)不用理會(huì ),一路回車(chē)*/ |
10 | Set DF bit in IP header? [no]: |
11 | Validate reply data? [no]: |
13 | Loose, Strict, Record, Timestamp, Verbose[none]: |
14 | Sweep range of sizes [n]: |
15 | Type escape sequence to abort. |
16 | Sending 10, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds: |
17 | Packet sent with a source address of 192.168.1.1 |
18 | !!!!!!!!!! /*可以看到ping通了十次*/ |
19 | Success rate is 100 percent (10/10), round-trip min/avg/max = 16/40/64 ms |
21 | /*高級ping命令能同樣能實(shí)現路由跟蹤*/ |
24 | Target IP address: 3.3.3.3 |
25 | Repeat count [5]: 1 /*Ping一次*/ |
27 | Timeout in seconds [2]: |
28 | Extended commands [n]: y /*使用擴展*/ |
29 | Source address or interface: 192.168.1.1 /*選擇源端口*/ |
31 | Set DF bit in IP header? [no]: |
32 | Validate reply data? [no]: |
34 | Loose, Strict, Record, Timestamp, Verbose[none]: r /*這里輸入r*/ |
36 | Loose, Strict, Record, Timestamp, Verbose[RV]: |
37 | Sweep range of sizes [n]: |
39 | /*記錄ICMP包從發(fā)出到返回所經(jīng)過(guò)的端口(外出方向)*/ |
40 | Reply to request 0 (96 ms). Received packet has options |
41 | Total option bytes= 40, padded length=40 |
43 | (12.1.1.1) /*首先從R1的這個(gè)接口發(fā)出*/ |
44 | (23.1.1.2) /*到達R2,R2從這個(gè)接口發(fā)出*/ |
46 | (23.1.1.3) /*ICMP包開(kāi)始返回,R3從這個(gè)接口發(fā)回給R2*/ |
47 | (12.1.1.2) /*到達R2,R2從這個(gè)接口發(fā)給R1*/ |
48 | (192.168.1.1) <*> /*回到起點(diǎn)*/ |
ping命令可以測試網(wǎng)絡(luò )通不通,但是如果中間網(wǎng)絡(luò )不通,ping不能很好的定位問(wèn)題出在哪里,而traceroute可以很好的定位問(wèn)題出現的位置,下面是正常狀態(tài)下traceroute 3.3.3.3的結果
3 | Type escape sequence to abort. |
4 | Tracing the route to 3.3.3.3 |
6 | 1 12.1.1.2 40 msec 24 msec 40 msec |
7 | 2 23.1.1.3 28 msec 48 msec * |
10 | * 可以看到數據首先經(jīng)過(guò)了12.1.1.2,然后到達23.1.1.3 |
11 | * 3.3.3.3和23.1.1.3同處于R3上,所以追蹤到此完成 |
* Traceroute的工作原理:
首先,發(fā)送設備將數據包中的TTL設置成1,數據包會(huì )被第一條路由器丟棄,返回一個(gè)錯誤碼信息,設備源根據這個(gè)信息判斷經(jīng)過(guò)的中間設備和延時(shí),設備源一般發(fā)送三個(gè)重復的包(這就是為什么每個(gè)IP后面有3個(gè)返回時(shí)間的原因”12.1.1.2 40 msec 24 msec 40 msec”);之后設備源接著(zhù)發(fā)送TTL為2的數據包,再發(fā)送TTL為3的數據包,直到數據包達到目的地或者TTL=30為止。在正常情況下,除非路由存在環(huán)路,否則TTL不會(huì )超過(guò)30就到達目的地。
在PC(Windows設備)CMD下,命令是tracert:
2 | traceroute to 3.3.3.3, 64 hops max, press Ctrl+C to stop |
3 | 1 192.168.1.1 18.000 ms 9.000 ms 9.000 ms |
4 | 2 12.1.1.2 39.000 ms 29.000 ms 29.000 ms |
5 | 3 *23.1.1.3 58.000 ms (ICMP type:3, code:3, Destination port unreachable) |
8 | * 可以看到最后一個(gè)包返回了一個(gè)端口不可達, |
9 | * 其實(shí)源設備就是利用返回的是錯誤碼,還是端口不可達判斷是否到達了目的主機。 |
10 | * 如果是端口不可達,就說(shuō)明到達了主機 |
11 | * 詳見(jiàn)"TCP/IP詳解 卷一:協(xié)議" |
[*4*].常用排錯命令
1 | /*針對某接口,以太網(wǎng)接口可以看到MAC地址,帶寬,IP地址等*/ |
2 | R1#show interfaces fa 1/0 |
3 | FastEthernet1/0 is up, line protocol is up |
4 | Hardware is AmdFE, address is cc00.143c.0010 (bia cc00.143c.0010) |
5 | Internet address is 192.168.1.1/24 |
6 | MTU 1500 bytes, BW 100000 Kbit, DLY 100 usec, |
7 | reliability 255/255, txload 1/255, rxload 1/255 |
8 | Encapsulation ARPA, loopback not set |
2 | R1#show ip interface brief |
3 | Interface IP-Address OK? Method Status Protocol |
4 | Serial0/0 12.1.1.1 YES manual up up |
5 | Serial0/1 unassigned YES unset administratively down down |
6 | Serial0/2 unassigned YES unset administratively down down |
7 | Serial0/3 unassigned YES unset administratively down down |
8 | FastEthernet1/0 192.168.1.1 YES manual up up |
2 | R1#show controllers fa 1/0 |
3 | * 如果是串行接口可以看到如下一行,可以判斷接口是DCE端還是DTE端還有時(shí)鐘 |
4 | * cable type : V.11 (X.21) DCE cable, received clockrate 2015232 |
6 | R1#show controllers s 1/0 |
3 | Protocol Address Age (min) Hardware Addr Type Interface |
4 | Internet 192.168.1.1 - cc00.143c.0010 ARPA FastEthernet1/0 |
5 | Internet 192.168.1.2 42 0050.7966.6800 ARPA FastEthernet1/0 |
————————————————————————————————————
[**] 注:如文中未特別聲明轉載請注明出自: QingSword.COM