CiscoConfParse to json output - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: CiscoConfParse to json output (/thread-15924.html) |
CiscoConfParse to json output - anna - Feb-06-2019 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 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" RE: CiscoConfParse to json output - scidam - Feb-07-2019 You need to insert some initialization of your dict before loop in line # 39: The code should look like the following: # .... result["interfaces"][intf_name].update({ "address": "n/a", "netmask": "n/a", "network": "n/a" }) 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 }) # ... RE: CiscoConfParse to json output - anna - Feb-07-2019 thanks, will try and let you know. RE: CiscoConfParse to json output - anna - Feb-08-2019 why this printing on screen json format, however file dumps is not working. dumping code data = json.dumps(result, indent=4) s = json.dumps(data) open("out.json","w").write(s)sample output
RE: CiscoConfParse to json output - scidam - Feb-09-2019 with open("out.json", "w") as f: f.write(json.dumps(result, indent=4)) RE: CiscoConfParse to json output - anna - Feb-09-2019 thanks, its working RE: CiscoConfParse to json output - anna - Feb-12-2019 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"]) not able to print in required format. Please help
|