python ciscoconfparse接口ips和它们的wan ips

时间:2019-02-09 05:24:53

标签: python-3.x

下面是我的示例conf文件,我想解析接口详细信息,例如接口名称,描述,VLAN类型,qos策略,IP地址,还想通过接口IP地址标识WAN IP池。


接口Aux0 / 0/1
链接协议ppp
撤消shutdown


接口Eth-Trunk1
对DEL-CON-S5328EI-SW-A5 Eth-Trunk2的描述
模式lacp-static
lacp抢先启用
最大活动链接数1
lacp抢占延迟10
统计启用


接口Eth-Trunk1.50
 统计启用
# 接口Eth-Trunk1.120
 vlan型dot1q 120
 说明EXT_COGENT E SERVICES PRIVATE LIMITED_12005744750_50MB
 ip地址111.93.43.217 255.255.255.252
 流量策略INFRA-ACL入站
 qos-profile 50Mbps入站标识符无
 qos-profile 50Mbps出站标识符无
 统计启用


接口Eth-Trunk1.123
 vlan型dot1q 123
 说明EXT_ILL_SARALA-HANDICRAFTS_PANIPAT_5018027739
 ip地址182.156.211.161 255.255.255.252
 流量策略INFRA-ACL入站
 用户队列cir 10240 pir 10400入站
 用户队列cir 10240 pir 10400出站
 统计启用


ip route-static 111.93.39.244 255.255.255.252 Eth-Trunk1.123 182.156.211.162说明SARALA-HANDICRAFTS_5018027739

1 个答案:

答案 0 :(得分:0)

示例代码,但是无法正确打印输出

from ciscoconfparse import CiscoConfParse
from pprint import pprint
parse = CiscoConfParse("huwaieconf.txt")
allInterfaces = {}
interfaces = {}
intfs = parse.find_objects_w_parents(r'^interface',r'Eth')
for intfobj in intfs:
    intf_name = intfobj.text.strip()
    #print (intf_name)
    interfaces.update({'name': intf_name})
    descr = intfobj.re_match_iter_typed((r" description "), group=1)
    interfaces.update({'description': descr})
    ip_addr = intfobj.re_match_iter_typed((r"ip\saddress\s(\S+\s+\S+)"),group=1)
    interfaces.update({'IP':ip_addr})
    print (interfaces)
  

{'name':'description EXT / ILL / 784395 / INVESTOPAD / 12405784395 / New Delhi / 30Mbps / Ethernet','description':'','IP':''}
  {'name':'description EXT / ILL / 782226 / NYT NEWS BUREAU印度私人有限公司/ 1105782226 / New Delhi / Ethernet','description':','IP':'}}
  {'name':'description EXT / ILL / 784459 / DYNAMIC Drilling and Services PRIVATE LIMITED / 1105784459/16 Mbps / New Delhi / Ethernet','description':','IP':”}
< / p>

请建议更改

相关问题