免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1981 | 回复: 9
打印 上一主题 下一主题

[函数] 【re】 erlang解析cisco config [复制链接]

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-07-03 11:42 |只看该作者 |倒序浏览
本帖最后由 shijiang1130 于 2015-02-03 23:13 编辑
  1. parse(X,H)->
  2.         R=binary:split(X,<<"!\r\n">>,[global]), [list(B,H) || B <- R].

  3. list(X,H) ->
  4.         case size(X) > 9 of
  5.                 true ->
  6.                         <<A:9/binary,_/binary>> = X,
  7.                       case A == <<"interface">> of
  8.                               false -> ok;
  9.                               true ->
  10.                                       X1 =binary:split(X,<<"\r\n">>,[]),
  11.                                         Interface=binary:replace(lists:nth(1,X1),<<"interface ">>,<<>>),
  12.                                       case re:run(X,<<"description (.*)\r">>,[{capture,[1],list}]) of
  13.                                               {match,[Desc]} -> ok;
  14.                                               nomatch        -> Desc = 0
  15.                                       end,
  16.                                       case re:run(X,<<"no cdp">>,[]) of
  17.                                               {match,_} -> Cdp = 1;
  18.                                               nomatch   -> Cdp = 0
  19.                                       end,
  20.                                       case re:run(X,<<"speed (.*)\r">>,[{capture,[1],list}]) of
  21.                                               {match,[Speed]} -> ok;
  22.                                               nomatch         -> Speed = 0
  23.                                       end,                                                                           
  24.                         case re:run(X,<<"switchport mode (\\w+)">>,[{capture,[1],list}]) of
  25.                                 {match,[Mode]}  -> ok;
  26.                                 _               -> Mode = 0
  27.                         end,
  28.                         case re:run(X,<<"spanning-tree portfast">>,[]) of
  29.                                 {match,_}       ->        Portfast=1;
  30.                                 _               ->        Portfast=0
  31.                         end,
  32.                         case re:run(X,<<"spanning-tree bpduguard enable">>,[]) of
  33.                                 {match,_}       ->        Bpdu=1;
  34.                                 _               ->  Bpdu=0
  35.                         end,
  36.                         Interface1 = binary_to_list(Interface),
  37.                         Id = H++"-"++ Interface1,                                           
  38.                                       F=fun()->  
  39.                                               Row = #running_config{id=Id,interface=Interface1,hostname=H,
  40.                                               desc=Desc,speed=Speed,switchport=Mode,cdp=Cdp,portfast=Portfast,
  41.                                               bpdu=Bpdu,score=binary_to_list(lists:nth(2,X1))},
  42.                                               mnesia:write(Row)  
  43.                                       end,
  44.                                     mnesia:transaction(F)
  45.                         end;
  46.                 false ->
  47.                            ok
  48.         end.       
复制代码

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
2 [报告]
发表于 2014-07-03 13:06 |只看该作者
哪种config ?

show config 出来的那个配置文件?

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
3 [报告]
发表于 2014-07-03 13:37 |只看该作者
q1208c 发表于 2014-07-03 13:06
哪种config ?

show config 出来的那个配置文件?
是的,show runn config 要能抽出interface,对比interface.

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
4 [报告]
发表于 2014-07-03 15:58 |只看该作者
回复 3# shijiang1130


给个测试文件      

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
5 [报告]
发表于 2014-07-03 16:15 |只看该作者
q1208c 发表于 2014-07-03 15:58
回复 3# shijiang1130
  1. Building configuration...

  2. Current configuration : 1384 bytes
  3. !
  4. version 12.4
  5. service timestamps debug datetime msec
  6. service timestamps log datetime msec
  7. no service password-encryption
  8. !
  9. hostname R1
  10. !
  11. boot-start-marker
  12. boot-end-marker
  13. !
  14. !
  15. no aaa new-model
  16. memory-size iomem 5
  17. no ip icmp rate-limit unreachable
  18. ip cef
  19. !
  20. !
  21. !
  22. !
  23. no ip domain lookup
  24. ip domain name t.cn
  25. !
  26. !
  27. !
  28. !
  29. !
  30. !
  31. !
  32. !
  33. !
  34. !
  35. !
  36. !
  37. !
  38. !
  39. !
  40. !
  41. !
  42. username admin password 0 admin
  43. !
  44. !
  45. ip tcp synwait-time 5
  46. !
  47. !
  48. !
  49. !
  50. !
  51. interface FastEthernet0/0
  52. ip address 172.16.1.1 255.255.255.0
  53. duplex auto
  54. speed auto
  55. !
  56. interface FastEthernet0/1
  57. no ip address
  58. shutdown
  59. duplex auto
  60. speed auto
  61. !
  62. interface FastEthernet1/0
  63. !
  64. interface FastEthernet1/1
  65. !
  66. interface FastEthernet1/2
  67. !
  68. interface FastEthernet1/3
  69. !
  70. interface FastEthernet1/4
  71. !
  72. interface FastEthernet1/5
  73. description R1:f1/5:R2:f1/5
  74. !
  75. interface FastEthernet1/6
  76. !
  77. interface FastEthernet1/7
  78. !
  79. interface FastEthernet1/8
  80. !
  81. interface FastEthernet1/9
  82. !
  83. interface FastEthernet1/10
  84. !
  85. interface FastEthernet1/11
  86. !
  87. interface FastEthernet1/12
  88. !
  89. interface FastEthernet1/13
  90. !
  91. interface FastEthernet1/14
  92. !
  93. interface FastEthernet1/15
  94. !
  95. interface Vlan1
  96. no ip address
  97. !
  98. !
  99. !
  100. !
  101. no ip http server
  102. no ip http secure-server
  103. !
  104. !
  105. !
  106. !
  107. !
  108. control-plane
  109. !
  110. !
  111. !
  112. !
  113. !
  114. !
  115. !
  116. !
  117. !
  118. !
  119. line con 0
  120. exec-timeout 0 0
  121. privilege level 15
  122. logging synchronous
  123. line aux 0
  124. exec-timeout 0 0
  125. privilege level 15
  126. logging synchronous
  127. line vty 0 4
  128. login local
  129. line vty 5 10
  130. login local
  131. !
  132. !
  133. end
复制代码

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
6 [报告]
发表于 2014-07-03 18:36 |只看该作者
kao, 你这配置也太简单了, 只有一个 interface 有配置, 其它的全是空的.

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
7 [报告]
发表于 2014-07-11 09:59 |只看该作者
回复 3# shijiang1130

你要对比 interface 的 config 变化么?
   

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
8 [报告]
发表于 2014-07-11 14:05 |只看该作者
q1208c 发表于 2014-07-11 09:59
回复 3# shijiang1130

你要对比 interface 的 config 变化么?
是的,就是哪个接口配置了,哪个是空的,配置了的,是不是都配置全了,有没有配置vlan

论坛徽章:
33
荣誉会员
日期:2011-11-23 16:44:17天秤座
日期:2014-08-26 16:18:20天秤座
日期:2014-08-29 10:12:18丑牛
日期:2014-08-29 16:06:45丑牛
日期:2014-09-03 10:28:58射手座
日期:2014-09-03 16:01:17寅虎
日期:2014-09-11 14:24:21天蝎座
日期:2014-09-17 08:33:55IT运维版块每日发帖之星
日期:2016-04-17 06:23:27操作系统版块每日发帖之星
日期:2016-04-18 06:20:00IT运维版块每日发帖之星
日期:2016-04-24 06:20:0015-16赛季CBA联赛之天津
日期:2016-05-06 12:46:59
9 [报告]
发表于 2014-07-11 14:33 |只看该作者
回复 8# shijiang1130

好的. 我试试.


这几天在忙着学习中
   

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
10 [报告]
发表于 2015-02-03 23:14 |只看该作者
整理一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP