Python Forum
CiscoConfParse to json output
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CiscoConfParse to json output
#1
Hi All,

I am trying to capture Interface,Description, Vlan,IP address,Subnet mark from huawei router configuration file.

"""
example script how to extract parameters from a Cisco IOS configuration using ciscoconfparse
"""
import json
from ciscoconfparse import CiscoConfParse
from ciscoconfparse.ccp_util import IPv4Obj

if __name__ == "__main__":
    # the result dictionary
    result = {
        "features": [],
        "interfaces": {}
    }

    confparse = CiscoConfParse("huwaieconf.txt")

    interface_cmds = confparse.find_objects(r"^interface ")

    # iterate over the resulting IOSCfgLine objects
    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] = {}

        # search for the description command, if not set use "not set" as value
        result["interfaces"][intf_name]["description"] = "not set"
        for cmd in interface_cmd.re_search_children(r"^ description "):
            result["interfaces"][intf_name]["description"] = cmd.text.strip()[len("description "):]

        result["interfaces"][intf_name]["qos-profile"] = "n/a"
        for cmd in interface_cmd.re_search_children(r"^ qos-profile "):
            result["interfaces"][intf_name]["qos-profile"] = cmd.text.strip()[len("qos-profile"):]
        result["interfaces"][intf_name]["vlan-type"] = "n/a"
        for cmd in interface_cmd.re_search_children(r"^ vlan-type "):
            result["interfaces"][intf_name]["vlan-type"]  = cmd.text.strip()[len("vlan-type"):]

        IPv4_REGEX = r"ip\saddress\s(\S+\s+\S+)"

        for cmd in interface_cmd.re_search_children(IPv4_REGEX):
            # ciscoconfparse provides a helper function for this task
            ipv4_addr = interface_cmd.re_match_iter_typed(IPv4_REGEX, result_type=IPv4Obj)

            result["interfaces"][intf_name].update({
                  "address": ipv4_addr.ip.exploded,
                  "netmask": ipv4_addr.netmask.exploded,
                  "network": ipv4_addr.network.exploded
            })

    print("\nEXTRACTED PARAMETERS\n")
    print(json.dumps(result, indent=4))
    data = json.dumps(result, indent=4)
    s = json.dumps(data)
    open("out.json","w").write(s)
sample output as below

Output:
EXTRACTED PARAMETERS { "features": [], "interfaces": { "Aux0/0/1": { "description": "not set", "qos-profile": "n/a", "vlan-type": "n/a" }, "Eth-Trunk1": { "description": "TO DEL-CON-S5328EI-SW-A5 Eth-Trunk2", "qos-profile": "n/a", "vlan-type": "n/a" }, "Eth-Trunk1.50": { "description": "not set", "qos-profile": "n/a", "vlan-type": "n/a" }, "Eth-Trunk1.103": { "description": "EXT_ILL_TTSL_Bhopal_5075509146", "qos-profile": " 6Mb outbound identifier none", "vlan-type": " dot1q 103", "address": "111.93.33.9", "netmask": "255.255.255.252", "network": "111.93.33.8/30" }, "Eth-Trunk1.120": { "description": "EXT_COGENT E SERVICES PRIVATE LIMITED_12005744750_50MB", "qos-profile": " 50Mbps outbound identifier none", "vlan-type": " dot1q 120", "address": "111.93.43.217", "netmask": "255.255.255.252", "network": "111.93.43.216/30" }, } }
my issue is, if interface is not having ip address, output should be printed as

"Eth-Trunk1.120": {
"description": "EXT_COGENT E SERVICES PRIVATE LIMITED_12005744750_50MB",
"qos-profile": " 50Mbps outbound identifier none",
"vlan-type": " dot1q 120",
"address": "n/a",
"netmask": "n/a",
"network": "na"
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,793 Mar-06-2022, 08:34 PM
Last Post: snippsat
  json.dumps list output qurr 12 5,262 Apr-08-2020, 10:13 PM
Last Post: micseydel
  json.dumps output error in python3 prayuktibid 2 2,661 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Output to a json file problem Netcode 3 3,744 Nov-22-2019, 01:44 AM
Last Post: Skaperen
  Make a table from a json output carstenlp 13 25,874 Jan-12-2019, 09:06 PM
Last Post: carstenlp
  Unable to parse JSON output dragan979 1 3,556 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