Feb-06-2019, 01:35 PM
Hi All,
I am trying to capture Interface,Description, Vlan,IP address,Subnet mark from huawei router configuration file.
"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"
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"