本帖最后由 shijiang1130 于 2015-02-03 23:13 编辑
- parse(X,H)->
- R=binary:split(X,<<"!\r\n">>,[global]), [list(B,H) || B <- R].
- 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,[1],list}]) of
- {match,[Desc]} -> 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,[1],list}]) of
- {match,[Speed]} -> ok;
- nomatch -> Speed = 0
- end,
- case re:run(X,<<"switchport mode (\\w+)">>,[{capture,[1],list}]) of
- {match,[Mode]} -> 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.
复制代码 |