打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Ping、Traceroute应用与排错

这篇文章通过一个实例,演示ping、traceroute、debug等命令的用法;测试网络连通性,分析网络数据不可达的原因。

___________________________________________

文章目录

___________________________________________

[*1*].实例拓扑图与配置

C1是使用VPCS虚拟出来的一台计算机,SW1是一台不可网管交换机,R1、R2、R3上都添加了”NM-4T”串口,R1上还添加了一个”NM-1FE-TX”快速以太网端口。R3上面配置了一个回环接口3.3.3.3/24,各设备IP配置和连接如下图:

R1配置如下:

1Router>en
2Router#conf t
3Router(config)#host R1
4R1(config)#line co 0
5R1(config-line)#logg syn
6R1(config-line)#exec-time 0 0
7R1(config-line)#exit
8R1(config)#int fa 1/0
9R1(config-if)#ip add 192.168.1.1 255.255.255.0
10R1(config-if)#no shut
11R1(config-if)#int s 0/0
12R1(config-if)#ip add 12.1.1.1 255.255.255.0
13R1(config-if)#no shut
14R1(config-if)#end
15R1#

R2配置如下:

1Router>en
2Router#conf t
3Router(config)#line co 0
4Router(config-line)#logg syn
5Router(config-line)#exec-time 0 0
6Router(config-line)#exit
7Router(config)#host R2
8R2(config)#int s 0/1
9R2(config-if)#ip add 12.1.1.2 255.255.255.0
10R2(config-if)#no shut
11R2(config-if)#int s 0/2
12R2(config-if)#ip add 23.1.1.2 255.255.255.0
13R2(config-if)#no shut
14R2(config-if)#end
15R2#

R3配置如下:

1Router>en
2Router#conf t
3Router(config)#host R3
4R3(config)#line co 0
5R3(config-line)#logg syn
6R3(config-line)#exec-time 0 0
7R3(config-line)#int s 0/3
8R3(config-if)#ip add 23.1.1.3 255.255.255.0
9R3(config-if)#no shut
10R3(config-if)#exit
11R3(config)#int loopback 0 /*配置回环接口0*/
12R3(config-if)#ip add 3.3.3.3 255.255.255.0
13R3(config-if)#no shut
14R3(config-if)#end
15R3#

[*2*].测试连通性

首先在C1上面测试ping自己的网关(R1的fa1/0接口)

1VPCS[1]> ping 192.168.1.1
2192.168.1.1 icmp_seq=1 ttl=255 time=49.000 ms /*可以ping通*/
3 
4/*继续在C1上测试ping不同网段的地址*/
5 
6VPCS[1]> ping 12.1.1.1
712.1.1.1 icmp_seq=1 ttl=255 time=29.000 ms /*ping网关上的串口,成功*/
8 
9VPCS[1]> ping 12.1.1.2 /*ping R2和R1相连的接口,失败,超时*/
1012.1.1.2 icmp_seq=1 timeout

这一步为什么会超时呢?是不是数据包没有到达R2?我们在R2上开启debug命令进行调试:

1R2#debug ip icmp
2ICMP packet debugging is on
3 
4R2# /*开启调试后,再一次用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
6 
7/*关闭所有调试的方法是undebug all*/

从上面的显示可以看出R2收到了C1发送过来的ICMP包,我们查看一下R2的路由表:

1R2#show ip route
2 
3/*省略部分输出*/
4 
5Gateway of last resort is not set
6 
723.0.0.0/24 is subnetted, 1 subnets
8C 23.1.1.0 is directly connected, Serial0/2
9 12.0.0.0/24 is subnetted, 1 subnets
10C 12.1.1.0 is directly connected, Serial0/1
11 
12R2#
13 
14/*
15 * 从输出可以明显的看到R2的路由表中有两个直连条目(C)
16 * 发往23.1.1.0/24网段的数据从Serial0/2发出
17 * 发往12.1.1.0/24网段的数据从Serial0/1发出
18 */

这样就很清楚的发现,并没有说明发往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通了:

1R2(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回复一个主机不可达:

1VPCS[1]> ping 3.3.3.3
2*192.168.1.1 icmp_seq=1 ttl=255 time=20.000 ms (ICMP type:3, code:1, Destination host unreachable)
3 
4VPCS[1]> ping 23.1.1.2
5*192.168.1.1 icmp_seq=1 ttl=255 time=20.000 ms (ICMP type:3, code:1, Destination host unreachable)
6 
7VPCS[1]> ping 23.1.1.3
8*192.168.1.1 icmp_seq=1 ttl=255 time=26.000 ms (ICMP type:3, code:1, Destination host unreachable)

在R1上开启ICMP调试就能看到

1R1#debug ip icmp
2ICMP packet debugging is on
3R1#
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)*/
2R1(config)#ip route 0.0.0.0 0.0.0.0 12.1.1.2
3 
4/*R2添加下面两条静态路由*/
5R2(config)#ip route 192.168.1.0 255.255.255.0 12.1.1.1
6R2(config)#ip route 3.3.3.0 255.255.255.0 23.1.1.3
7 
8/*R3也添加一条默认路由*/
9R3(config)#ip route 0.0.0.0 0.0.0.0 23.1.1.2

这个时候,不论在哪个设备上,都能ping通拓扑上的所有接口IP。下面是R1上面ping R3回环接口的返回信息:

1R1#ping 3.3.3.3
2 
3Type escape sequence to abort.
4Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
5!!!!!
6 
7/*
8 * 这里显示五个感叹号,说明上面默认ping发送5次,ping成功5次
9 * 如果超时会显示省略号"....."
10 */

关闭R3的回环接口,就会出现超时的情况:

1R3#conf t
2R3(config)#int lo 0
3R3(config-if)#shut      /*关闭R3的回环接口*/
4R3(config-if)#
5 
6/*使用R1去ping,显示超时,实际上这里产生了路由环路*/
7R1#ping 3.3.3.3
8 
9Type escape sequence to abort.
10Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
11.
12*Mar 1 00:45:09.355: ICMP: time exceeded rcvd from 23.1.1.2.
13 
14/*R2显示的信息,意思是TTL=0了*/
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

1/*关闭R2与R3相连的s0/2接口*/
2R2(config-if)#int s 0/2
3R2(config-if)#shut
4 
5/*R1开始ping*/
6R1#ping 3.3.3.3
7 
8Type escape sequence to abort.
9Sending 5, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
10 
11U   /*这里显示不可达(U)*/
12 
13*Mar 1 00:53:32.091: ICMP: dst (12.1.1.1) host unreachable rcv from 12.1.1.2
14 
15/*R2上的显示如下*/
16R2#
17*Mar 1 00:53:34.823: ICMP: dst (3.3.3.3) host unreachable sent to 12.1.1.1
18 
19/*
20 * 查看R2路由表,发现直连接口条目23.1.1.0/24消失了
21 * 同样,目的地址是这个直连接口网段的静态路由条目也消失了
22 * 所以R2返回一个消息告诉R1"(3.3.3.3) host unreachable"
23 */
24R2#show ip route
25 
26Gateway of last resort is not set
27 
2812.0.0.0/24 is subnetted, 1 subnets
29C 12.1.1.0 is directly connected, Serial0/1
30S 192.168.1.0/24 [1/0] via 12.1.1.1

打开R2的s0/2继续下面的实验。

[*3*].高级ping命令和路由追踪命令traceroute

使用高级ping命令,R1 ping R3的回环接口:

1R1#ping              /*直接输入ping,回车*/
2Protocol [ip]:       /*选择协议,默认回车即可*/
3Target IP address: 3.3.3.3    /*选择目标IP,这里是R3的lo0接口IP*/
4Repeat count [5]: 10          /*ping次数,这里输入了10次,默认5次*/
5Datagram size [100]:         /*数据包大小,默认回车即可*/
6Timeout in seconds [2]:      /*超时时间,直接回车*/
7Extended commands [n]: y      /*是否显示扩展命令,输入y*/
8Source address or interface: 192.168.1.1 /*选择用本地的哪个接口去ping,这里选择R1的以太网接口*/
9Type of service [0]:         /*下面暂时不用理会,一路回车*/
10Set DF bit in IP header? [no]:
11Validate reply data? [no]:
12Data pattern [0xABCD]:
13Loose, Strict, Record, Timestamp, Verbose[none]:
14Sweep range of sizes [n]:
15Type escape sequence to abort.
16Sending 10, 100-byte ICMP Echos to 3.3.3.3, timeout is 2 seconds:
17Packet sent with a source address of 192.168.1.1
18!!!!!!!!!!            /*可以看到ping通了十次*/
19Success rate is 100 percent (10/10), round-trip min/avg/max = 16/40/64 ms
20 
21/*高级ping命令能同样能实现路由跟踪*/
22R1#ping
23Protocol [ip]:
24Target IP address: 3.3.3.3
25Repeat count [5]: 1   /*Ping一次*/
26Datagram size [100]:
27Timeout in seconds [2]:
28Extended commands [n]: y   /*使用扩展*/
29Source address or interface: 192.168.1.1 /*选择源端口*/
30Type of service [0]:
31Set DF bit in IP header? [no]:
32Validate reply data? [no]:
33Data pattern [0xABCD]:
34Loose, Strict, Record, Timestamp, Verbose[none]: r  /*这里输入r*/
35Number of hops [ 9 ]:
36Loose, Strict, Record, Timestamp, Verbose[RV]:
37Sweep range of sizes [n]:
38 
39/*记录ICMP包从发出到返回所经过的端口(外出方向)*/
40Reply to request 0 (96 ms). Received packet has options
41 Total option bytes= 40, padded length=40
42 Record route:
43 (12.1.1.1)  /*首先从R1的这个接口发出*/
44 (23.1.1.2)  /*到达R2,R2从这个接口发出*/
45 (3.3.3.3)   /*到达目的地*/
46 (23.1.1.3)  /*ICMP包开始返回,R3从这个接口发回给R2*/
47 (12.1.1.2)  /*到达R2,R2从这个接口发给R1*/
48 (192.168.1.1) <*>  /*回到起点*/
49 (0.0.0.0)
50 (0.0.0.0)
51 (0.0.0.0)
52 End of list

ping命令可以测试网络通不通,但是如果中间网络不通,ping不能很好的定位问题出在哪里,而traceroute可以很好的定位问题出现的位置,下面是正常状态下traceroute 3.3.3.3的结果

1R1#traceroute 3.3.3.3
2 
3Type escape sequence to abort.
4Tracing the route to 3.3.3.3
5 
61 12.1.1.2 40 msec 24 msec 40 msec
7 2 23.1.1.3 28 msec 48 msec *
8 
9/*
10 * 可以看到数据首先经过了12.1.1.2,然后到达23.1.1.3
11 * 3.3.3.3和23.1.1.3同处于R3上,所以追踪到此完成
12 */

* 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:

1PC> tracert 3.3.3.3 -d
2traceroute 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)
6 
7/*
8 * 可以看到最后一个包返回了一个端口不可达,
9 * 其实源设备就是利用返回的是错误码,还是端口不可达判断是否到达了目的主机。
10 * 如果是端口不可达,就说明到达了主机
11 * 详见"TCP/IP详解 卷一:协议"
12 */

[*4*].常用排错命令

1/*针对某接口,以太网接口可以看到MAC地址,带宽,IP地址等*/
2R1#show interfaces fa 1/0
3FastEthernet1/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

 

1/*查看接口是否开启*/
2R1#show ip interface brief
3Interface IP-Address OK? Method Status Protocol
4Serial0/0 12.1.1.1 YES manual up up
5Serial0/1 unassigned YES unset administratively down down
6Serial0/2 unassigned YES unset administratively down down
7Serial0/3 unassigned YES unset administratively down down
8FastEthernet1/0 192.168.1.1 YES manual up up

 

1/*查看路由器IOS版本,硬件信息等*/
2R1#show version

 

1/*查看路由器接口硬件信息*/
2R1#show controllers fa 1/0

 

1/*
2 * 查看路由器接口硬件信息,
3 * 如果是串行接口可以看到如下一行,可以判断接口是DCE端还是DTE端还有时钟
4 * cable type : V.11 (X.21) DCE cable, received clockrate 2015232
5 */
6R1#show controllers s 1/0

 

1/*查看路由器ARP缓存*/
2R1#show arp
3Protocol Address Age (min) Hardware Addr Type Interface
4Internet 192.168.1.1 - cc00.143c.0010 ARPA FastEthernet1/0
5Internet 192.168.1.2 42 0050.7966.6800 ARPA FastEthernet1/0

————————————————————————————————————

[**] 注:如文中未特别声明转载请注明出自: QingSword.COM

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ping和traceroute原理分析
试试这个命令,比ping还好用
网络设备排障怎么破?这五个命令申请出战!
卡卡笔记之Linux网络管理----命令详解 - Kachy - 51CTO技术博客
运维之思科篇-NAT基础配置
路由器------逻辑端口
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服