shijiang1130 发表于 2014-07-03 11:42

【re】 erlang解析cisco config

本帖最后由 shijiang1130 于 2015-02-03 23:13 编辑

parse(X,H)->
        R=binary:split(X,<<"!\r\n">>,), .

list(X,H) ->
        case size(X) > 9 of
                true ->
                        <<A:9/binary,_/binary>> = X,
                      case A == <<"interface">> of
                              false -> ok;
                              true ->
                                      X1 =binary:split(X,<<"\r\n">>,[]),
                                        Interface=binary:replace(lists:nth(1,X1),<<"interface ">>,<<>>),
                                      case re:run(X,<<"description (.*)\r">>,[{capture,,list}]) of
                                              {match,} -> ok;
                                              nomatch      -> Desc = 0
                                      end,
                                      case re:run(X,<<"no cdp">>,[]) of
                                              {match,_} -> Cdp = 1;
                                              nomatch   -> Cdp = 0
                                      end,
                                      case re:run(X,<<"speed (.*)\r">>,[{capture,,list}]) of
                                              {match,} -> ok;
                                              nomatch         -> Speed = 0
                                      end,                                                                           
                      case re:run(X,<<"switchport mode (\\w+)">>,[{capture,,list}]) of
                              {match,}-> ok;
                              _               -> Mode = 0
                      end,
                      case re:run(X,<<"spanning-tree portfast">>,[]) of
                              {match,_}       ->        Portfast=1;
                              _               ->        Portfast=0
                      end,
                      case re:run(X,<<"spanning-tree bpduguard enable">>,[]) of
                              {match,_}       ->        Bpdu=1;
                              _               ->Bpdu=0
                      end,
                      Interface1 = binary_to_list(Interface),
                      Id = H++"-"++ Interface1,                                         
                                      F=fun()->
                                              Row = #running_config{id=Id,interface=Interface1,hostname=H,
                                              desc=Desc,speed=Speed,switchport=Mode,cdp=Cdp,portfast=Portfast,
                                              bpdu=Bpdu,score=binary_to_list(lists:nth(2,X1))},
                                              mnesia:write(Row)
                                      end,
                                    mnesia:transaction(F)
                        end;
                false ->
                           ok
        end.       

q1208c 发表于 2014-07-03 13:06

哪种config ?

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

shijiang1130 发表于 2014-07-03 13:37

q1208c 发表于 2014-07-03 13:06 static/image/common/back.gif
哪种config ?

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

q1208c 发表于 2014-07-03 15:58

回复 3# shijiang1130


给个测试文件 :mrgreen:   

shijiang1130 发表于 2014-07-03 16:15

q1208c 发表于 2014-07-03 15:58 static/image/common/back.gif
回复 3# shijiang1130


Building configuration...

Current configuration : 1384 bytes
!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname R1
!
boot-start-marker
boot-end-marker
!
!
no aaa new-model
memory-size iomem 5
no ip icmp rate-limit unreachable
ip cef
!
!
!
!
no ip domain lookup
ip domain name t.cn
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
username admin password 0 admin
!
!
ip tcp synwait-time 5
!
!
!
!
!
interface FastEthernet0/0
ip address 172.16.1.1 255.255.255.0
duplex auto
speed auto
!
interface FastEthernet0/1
no ip address
shutdown
duplex auto
speed auto
!
interface FastEthernet1/0
!
interface FastEthernet1/1
!
interface FastEthernet1/2
!
interface FastEthernet1/3
!
interface FastEthernet1/4
!
interface FastEthernet1/5
description R1:f1/5:R2:f1/5
!
interface FastEthernet1/6
!
interface FastEthernet1/7
!
interface FastEthernet1/8
!
interface FastEthernet1/9
!
interface FastEthernet1/10
!
interface FastEthernet1/11
!
interface FastEthernet1/12
!
interface FastEthernet1/13
!
interface FastEthernet1/14
!
interface FastEthernet1/15
!
interface Vlan1
no ip address
!
!
!
!
no ip http server
no ip http secure-server
!
!
!
!
!
control-plane
!
!
!
!
!
!
!
!
!
!
line con 0
exec-timeout 0 0
privilege level 15
logging synchronous
line aux 0
exec-timeout 0 0
privilege level 15
logging synchronous
line vty 0 4
login local
line vty 5 10
login local
!
!
end

q1208c 发表于 2014-07-03 18:36

kao, 你这配置也太简单了, 只有一个 interface 有配置, 其它的全是空的.

q1208c 发表于 2014-07-11 09:59

回复 3# shijiang1130

你要对比 interface 的 config 变化么?
   

shijiang1130 发表于 2014-07-11 14:05

q1208c 发表于 2014-07-11 09:59 static/image/common/back.gif
回复 3# shijiang1130

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

q1208c 发表于 2014-07-11 14:33

回复 8# shijiang1130

好的. 我试试.


这几天在忙着学习中
   

shijiang1130 发表于 2015-02-03 23:14

整理一下:em17:
页: [1]
查看完整版本: 【re】 erlang解析cisco config