Feb-12-2019, 10:40 AM
tried another code as i am not able to parser json data.
from ciscoconfparse import CiscoConfParse from ciscoconfparse.ccp_util import IPv4Obj import re import ipaddress result = { "interfaces": {} } #interfaces = {} confparse = CiscoConfParse("huwaietest.conf") INTF_RE = re.compile(r'^interface\s\S+') ADDR_RE = re.compile(r'ip\saddress\s(\S+\s+\S+)') DESC_RE = re.compile(r'description ') VLAN_TYPE = re.compile(r'vlan-type') QOS_PROFILE = re.compile(r'qos-profile') interface_cmds = confparse.find_objects(r"^interface ") for interface_cmd in interface_cmds: # get the interface name (remove the interface command from the configuration line) intf_name = interface_cmd.text[len("interface "):] result["interfaces"][intf_name] = {} # if no vlan .. update as "not set" result["interfaces"][intf_name]["vlan_type"] = "not set" for vlan in interface_cmd.re_search_children(r"^ vlan-type"): result["interfaces"][intf_name]["vlan_type"] = vlan.text.strip()[len("vlan-type dot1q "):].split(' ')[0] # if no decrition... update as "not set" result["interfaces"][intf_name]["description"] = "not set" for descr in interface_cmd.re_search_children(r"^ description "): result["interfaces"][intf_name]["description"] = descr.text.strip()[len("description "):] result["interfaces"][intf_name]["ipv4"] = "not set" #result["interfaces"][intf_name].update({"ipv4": "not set"}) IPv4_REGEX = r"ip\saddress\s(\S+\s+\S+)" for ipv4_addr in interface_cmd.re_search_children(IPv4_REGEX): ipv4_addr = interface_cmd.re_match_iter_typed(IPv4_REGEX, result_type=IPv4Obj) result["interfaces"][intf_name]["ipv4"]= ipv4_addr.network.exploded result["interfaces"][intf_name]["qos_profile"] = "not set" for qos in interface_cmd.re_search_children(r"^ qos-profile"): result["interfaces"][intf_name]["qos_profile"] = qos.text.strip()[len("qos-profile "):].split(' ')[0] print(result["interfaces"])
Output:{'Eth-Trunk5': {'vlan_type': 'not set'}, 'Eth-Trunk6': {'vlan_type': 'not set'}, 'Eth-Trunk6.1000': {'vlan_type': '1000', 'description': 'connecting to DEL-CON-S6348F-SW-A5_XG0/0/22', 'ipv4': '10.117.138.24/30', 'qos_profile': 'not set'}, 'Eth-Trunk6.1069': {'vlan_type': 'not set'}, 'Eth-Trunk6.1163': {'vlan_type': '1163', 'description': 'description EXT/ILL/791445/NEXTRA TELESERVICES PRIVATE LIMITE/12405742920/3.1Gbps/P2P', 'ipv4': '111.93.126.148/30', 'qos_profile': '3100Mbps'}, 'Eth-Trunk6.1164': {'vlan_type': '1164', 'description': 'EXT/ILL/828320/WI-BRO SOLUTIONS PRIVATE LIMITED/1105828320/NEW DELHI/1000MB/P2M/3333', 'ipv4': '14.98.85.112/30', 'qos_profile': '1000Mbps'}, 'Virtual-Template0': {'vlan_type': 'not set'}, 'GigabitEthernet0/0/0': {'vlan_type': 'not set'}, 'GigabitEthernet1/0/0': {'vlan_type': 'not set'}, 'GigabitEthernet1/0/1': {'vlan_type': 'not set'}, 'GigabitEthernet1/0/2': {'vlan_type': 'not set'}, 'GigabitEthernet1/0/2.511': {'vlan_type': '511', 'description': 'forUBR_NMS-Spoke location', 'ipv4': '10.117.137.192/30', 'qos_profile': 'not set'}, 'GigabitEthernet1/0/2.705': {'vlan_type': '705', 'description': 'GOOGLE_PERING_DELHI_VSB_IGW_A3', 'ipv4': '192.168.237.192/30', 'qos_profile': 'not set'}, 'GigabitEthernet1/0/3': {'vlan_type': 'not set'}, 'GigabitEthernet1/0/4': {'vlan_type': 'not set'}, 'GigabitEthernet3/0/0': {'vlan_type': 'not set'}, 'GigabitEthernet3/0/1': {'vlan_type': 'not set'}, 'GigabitEthernet3/0/2': {'vlan_type': 'not set'}, 'GigabitEthernet3/0/3': {'vlan_type': 'not set'}, 'GigabitEthernet3/0/4': {'vlan_type': 'not set'}}
not able to print in required format. Please help