这篇文章通过一个实例,演示ping、traceroute、debug等命令的用法;测试网络连通性,分析网络数据不可达的原因。
___________________________________________
文章目录
___________________________________________
[*1*].实例拓扑图与配置
C1是使用VPCS虚拟出来的一台计算机,SW1是一台不可网管交换机,R1、R2、R3上都添加了”NM-4T”串口,R1上还添加了一个”NM-1FE-TX”快速以太网端口。R3上面配置了一个回环接口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 /*配置回环接口0*/ |
12 | R3(config-if)#ip add 3.3.3.3 255.255.255.0 |
[*2*].测试连通性
首先在C1上面测试ping自己的网关(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通*/ |
7 | 12.1.1.1 icmp_seq=1 ttl=255 time=29.000 ms /*ping网关上的串口,成功*/ |
9 | VPCS[1]> ping 12.1.1.2 /*ping R2和R1相连的接口,失败,超时*/ |
10 | 12.1.1.2 icmp_seq=1 timeout |
这一步为什么会超时呢?是不是数据包没有到达R2?我们在R2上开启debug命令进行调试:
2 | ICMP packet debugging is on |
4 | R2# /*开启调试后,再一次用C1去ping 12.1.1.2,发现R2上出现了下面的提示*/ |
5 | *Mar 1 00:17:10.135: ICMP: echo reply sent, src 12.1.1.2, dst 192.168.1.2 |
7 | /*关闭所有调试的方法是undebug all*/ |
从上面的显示可以看出R2收到了C1发送过来的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的路由表中有两个直连条目(C) |
16 | * 发往23.1.1.0/24网段的数据从Serial0/2发出 |
17 | * 发往12.1.1.0/24网段的数据从Serial0/1发出 |
这样就很清楚的发现,并没有说明发往192.168.1.0/24网段的数据应该怎么发送,所以R2丢弃发往192.168.1.2发送过来的数据。
下面给R2添加静态路由,让他知道发往192.168.1.0/24网段的数据应该发给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 |
这个时候实际上C1 ping R3的任何地址都是ping不通的,因为数据包到达R1后,R1检查自己的路由表,它会发现没有任何去往R3的路由条目(3.3.3.0/24、23.1.1.0/24),所以它会直接给C1回复一个主机不可达:
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上开启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 |
要想让这个拓扑图中的各个设备都能互相ping通,需要在R1、R2、R3上面添加下面的静态路由条目:
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 |
这个时候,不论在哪个设备上,都能ping通拓扑上的所有接口IP。下面是R1上面ping R3回环接口的返回信息:
3 | Type escape sequence to abort. |
4 | Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds: |
8 | * 这里显示五个感叹号,说明上面默认ping发送5次,ping成功5次 |
关闭R3的回环接口,就会出现超时的情况:
3 | R3(config-if)#shut /*关闭R3的回环接口*/ |
6 | /*使用R1去ping,显示超时,实际上这里产生了路由环路*/ |
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) |
实际上这里就是因为R3关闭了换回接口,R1首先根据自己的默认路由将数据发送给R2,R2再根据自己的静态路由将数据发送给R3,如果回环口没有关闭,R3将应答这个ICMP,但是此时R3上回环关闭了,R3根据自己的默认路由又将数据发回R2,R2再次将数据发回R3,这样直到IP报头里面的TTL字段减小到0,丢弃这个数据。
下面我们打开R3的lo0回环接口,关闭R2的s0/2,再次用R1去ping 3.3.3.3
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路由表,发现直连接口条目23.1.1.0/24消失了 |
21 | * 同样,目的地址是这个直连接口网段的静态路由条目也消失了 |
22 | * 所以R2返回一个消息告诉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 |
打开R2的s0/2继续下面的实验。
[*3*].高级ping命令和路由追踪命令traceroute
使用高级ping命令,R1 ping R3的回环接口:
2 | Protocol [ip]: /*选择协议,默认回车即可*/ |
3 | Target IP address: 3.3.3.3 /*选择目标IP,这里是R3的lo0接口IP*/ |
4 | Repeat count [5]: 10 /*ping次数,这里输入了10次,默认5次*/ |
5 | Datagram size [100]: /*数据包大小,默认回车即可*/ |
6 | Timeout in seconds [2]: /*超时时间,直接回车*/ |
7 | Extended commands [n]: y /*是否显示扩展命令,输入y*/ |
8 | Source address or interface: 192.168.1.1 /*选择用本地的哪个接口去ping,这里选择R1的以太网接口*/ |
9 | Type of service [0]: /*下面暂时不用理会,一路回车*/ |
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 |
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包从发出到返回所经过的端口(外出方向)*/ |
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的这个接口发出*/ |
44 | (23.1.1.2) /*到达R2,R2从这个接口发出*/ |
46 | (23.1.1.3) /*ICMP包开始返回,R3从这个接口发回给R2*/ |
47 | (12.1.1.2) /*到达R2,R2从这个接口发给R1*/ |
48 | (192.168.1.1) <*> /*回到起点*/ |
ping命令可以测试网络通不通,但是如果中间网络不通,ping不能很好的定位问题出在哪里,而traceroute可以很好的定位问题出现的位置,下面是正常状态下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 | * 可以看到数据首先经过了12.1.1.2,然后到达23.1.1.3 |
11 | * 3.3.3.3和23.1.1.3同处于R3上,所以追踪到此完成 |
* Traceroute的工作原理:
首先,发送设备将数据包中的TTL设置成1,数据包会被第一条路由器丢弃,返回一个错误码信息,设备源根据这个信息判断经过的中间设备和延时,设备源一般发送三个重复的包(这就是为什么每个IP后面有3个返回时间的原因”12.1.1.2 40 msec 24 msec 40 msec”);之后设备源接着发送TTL为2的数据包,再发送TTL为3的数据包,直到数据包达到目的地或者TTL=30为止。在正常情况下,除非路由存在环路,否则TTL不会超过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) |
9 | * 其实源设备就是利用返回的是错误码,还是端口不可达判断是否到达了目的主机。 |
[*4*].常用排错命令
1 | /*针对某接口,以太网接口可以看到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端还有时钟 |
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
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。