Python Forum
CiscoConfParse to json output
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CiscoConfParse to json output
#7
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
Reply


Messages In This Thread
CiscoConfParse to json output - by anna - Feb-06-2019, 01:35 PM
RE: CiscoConfParse to json output - by scidam - Feb-07-2019, 12:13 PM
RE: CiscoConfParse to json output - by anna - Feb-07-2019, 04:26 PM
RE: CiscoConfParse to json output - by anna - Feb-08-2019, 12:20 PM
RE: CiscoConfParse to json output - by scidam - Feb-09-2019, 11:45 AM
RE: CiscoConfParse to json output - by anna - Feb-09-2019, 05:18 PM
RE: CiscoConfParse to json output - by anna - Feb-12-2019, 10:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  geojson to json --missing multiple row output yoshi 9 2,732 Mar-06-2022, 08:34 PM
Last Post: snippsat
  json.dumps list output qurr 12 5,161 Apr-08-2020, 10:13 PM
Last Post: micseydel
  json.dumps output error in python3 prayuktibid 2 2,642 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Output to a json file problem Netcode 3 3,717 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Make a table from a json output carstenlp 13 25,698 Jan-12-2019, 09:06 PM
Last Post: carstenlp
  Unable to parse JSON output dragan979 1 3,532 Apr-20-2018, 02:24 PM
Last Post: dragan979

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020