您的位置:首页 > 其它

使用show ip route查看某个指定网段的所有内容

2012-02-05 11:27 537 查看
  show ip route可以说是网络工程师最常用的命令之一,对于查看路由指向、诊断网络问题很有帮助。对于一个大型网络,常常有成百数千条路由,如何才能找到自己需要的路由条目是一件很头疼的事情。

  为了解决这个办法,我们常用show ip route | include +网络前缀 的方法来找到匹配的条目。

10.0.0.0/24 is subnetted, 11 subnets
O       10.1.3.0 [110/20] via 10.1.1.1, 00:13:28, Ethernet0/0
O       10.2.1.0 [110/20] via 10.1.1.1, 00:13:28, Ethernet0/0
O       10.1.2.0 [110/20] via 10.1.1.1, 00:13:28, Ethernet0/0
O       10.2.2.0 [110/20] via 10.1.1.1, 00:13:28, Ethernet0/0
C       10.1.1.0 is directly connected, Ethernet0/0
O       10.3.2.0 [110/20] via 10.1.1.1, 00:13:28, Ethernet0/0
O       10.2.3.0 [110/20] via 10.1.1.1, 00:13:28, Ethernet0/0
O       10.4.2.0 [110/20] via 10.1.1.1, 00:13:30, Ethernet0/0
O       10.1.4.0 [110/20] via 10.1.1.1, 00:13:30, Ethernet0/0
C       10.1.30.0 is directly connected, Ethernet0/0
C       10.1.20.0 is directly connected, Ethernet0/0
O    192.168.1.0/24 [110/20] via 10.1.1.1, 00:13:30, Ethernet0/0
C    192.168.2.0/24 is directly connected, Ethernet0/0

  例如,在上面这张路由表中,我们可以用show ip route | i 192.168.1 很轻易地找到以192.168.1开头的所有条目。

R2#show ip route | i 192.168.1
O    192.168.1.0/24 [110/20] via 10.1.1.1, 00:15:46, Ethernet0/0

  但是对于目的地址也包含我们所需查找的网段时,这条命令就不这么好用了。
例如查找以10.1开头的所有网段:

R2#show ip route | i 10.1
O       10.1.3.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
O       10.2.1.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
O       10.1.2.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
O       10.2.2.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
C       10.1.1.0 is directly connected, Ethernet0/0
O       10.3.2.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
O       10.2.3.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
O       10.4.2.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
O       10.1.4.0 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0
C       10.1.30.0 is directly connected, Ethernet0/0
C       10.1.20.0 is directly connected, Ethernet0/0
O    192.168.1.0/24 [110/20] via 10.1.1.1, 00:18:32, Ethernet0/0

  由于大部分路由的目的地址都是10.1.1.1,所以结果中包含了很多条我们不需要的路由。
  在这种情况下我们可以使用show ip route的longer-prefixes参数来匹配我们的网段:show ip route 10.1.0.0 255.255.0.0 longer-prefixes

10.0.0.0/24 is subnetted, 11 subnets
O       10.1.3.0 [110/20] via 10.1.1.1, 00:21:16, Ethernet0/0
O       10.1.2.0 [110/20] via 10.1.1.1, 00:21:16, Ethernet0/0
C       10.1.1.0 is directly connected, Ethernet0/0
O       10.1.4.0 [110/20] via 10.1.1.1, 00:21:16, Ethernet0/0
C       10.1.30.0 is directly connected, Ethernet0/0
C       10.1.20.0 is directly connected, Ethernet0/0

  结果显示出了10.1.0.0/16及其子网的所有路由条目,符合我们的要求。

  当然我们也可利用show ip route list + 访问控制列表(ACL) 的方法来实现网段的匹配。

R2(config)#access-list 1 per 10.1.0.0 0.0.255.255
R2#sh ip route list 1
O       10.1.3.0 [110/20] via 10.1.1.1, 00:35:15, Ethernet0/0
O       10.1.2.0 [110/20] via 10.1.1.1, 00:35:15, Ethernet0/0
C       10.1.1.0 is directly connected, Ethernet0/0
O       10.1.4.0 [110/20] via 10.1.1.1, 00:35:15, Ethernet0/0
C       10.1.30.0 is directly connected, Ethernet0/0
C       10.1.20.0 is directly connected, Ethernet0/0

  使用ACL,我们可以实现多个网段的匹配。但是在工作环境中,仅仅为了查看路由表就在客户的机器上添加ACL也不太好,万一看完忘记删,或者改写了已有的ACL,也是一件很麻烦的事情。

终极方法:正则表达式
  正则表达式可以说是字符匹配的终极工具,思科的管道符“|”命令也支持正则表达式。下面将介绍如何使用正则表达式来匹配路由条目。
10.0.0.0/24 is subnetted, 11 subnets
O       10.1.3.0 [110/20] via 10.1.1.1, 00:42:53, Ethernet0/0
O       10.2.1.0 [110/20] via 10.1.1.1, 00:42:53, Ethernet0/0
O       10.1.2.0 [110/20] via 10.1.1.1, 00:42:53, Ethernet0/0
O       10.2.2.0 [110/20] via 10.1.1.1, 00:42:53, Ethernet0/0
C       10.1.1.0 is directly connected, Ethernet0/0
O       10.3.2.0 [110/20] via 10.1.1.1, 00:42:53, Ethernet0/0
O       10.2.3.0 [110/20] via 10.1.1.1, 00:42:53, Ethernet0/0
O       10.4.2.0 [110/20] via 10.1.1.1, 00:42:53, Ethernet0/0
O       10.1.4.0 [110/20] via 10.1.1.1, 00:42:55, Ethernet0/0
C       10.1.30.0 is directly connected, Ethernet0/0
C       10.1.20.0 is directly connected, Ethernet0/0
O    192.168.1.0/24 [110/20] via 10.1.1.1, 00:42:55, Ethernet0/0
C    192.168.2.0/24 is directly connected, Ethernet0/0

  仔细观察这张路由表,我们发现所有路由条目之前都至少有两个空格,我们就利用这个特点进行匹配。在正则表达式中我们可以利用“_”来匹配空格。
  例如,匹配10.1网段,我们可以使用如下命令:show ip route | i __10\.1\.

R2#sh ip route | i __10\.1\.
O       10.1.3.0 [110/20] via 10.1.1.1, 00:46:29, Ethernet0/0
O       10.1.2.0 [110/20] via 10.1.1.1, 00:46:29, Ethernet0/0
C       10.1.1.0 is directly connected, Ethernet0/0
O       10.1.4.0 [110/20] via 10.1.1.1, 00:46:29, Ethernet0/0
C       10.1.30.0 is directly connected, Ethernet0/0
C       10.1.20.0 is directly connected, Ethernet0/0

  由于“.”在正则表达式中是有特殊含义的(代表任意单个字符),我们必须用转义符“\”,让系统认为这是一个普通字符,否则会导致匹配不精确。
  我们还可以利用正则表达式实现多个网段的匹配,例如同时匹配10.1和10.2网段:show ip route | i __10\.(1|2)\.

R2#sh ip route | i __10\.(1|2)\.
O       10.1.3.0 [110/20] via 10.1.1.1, 00:01:02, Ethernet0/0
O       10.2.1.0 [110/20] via 10.1.1.1, 00:01:02, Ethernet0/0
O       10.1.2.0 [110/20] via 10.1.1.1, 00:01:02, Ethernet0/0
O       10.2.2.0 [110/20] via 10.1.1.1, 00:01:02, Ethernet0/0
C       10.1.1.0 is directly connected, Ethernet0/0
O       10.2.3.0 [110/20] via 10.1.1.1, 00:01:02, Ethernet0/0
O       10.1.4.0 [110/20] via 10.1.1.1, 00:01:02, Ethernet0/0
C       10.1.30.0 is directly connected, Ethernet0/0
C       10.1.20.0 is directly connected, Ethernet0/0

  以上只是正则表达式的一些简单应用,本人也只是正则表达式的初学者,要更加系统地了解cisco的正则表达式书写方法,请参阅:http://www.cisco.com/en/US/docs/ios/12_2/termserv/configuration/guide/tcfaapre_ps1835_TSD_Products_Configuration_Guide_Chapter.html
当然,正则表达式也是有局限性的(例如根据掩码长度匹配较难)。要根据自己的情况灵活运用各种方法,才能达到满意的结果。本文出自 “wwj的技术小站” 博客,请务必保留此出处http://wwj3528.blog.51cto.com/1017324/772842
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: