- Developing an app for IP address management. Have the folowing error when trying to print out (or write to file) the IP address of a device.
KeyError: 'ipv4Mask'
It seems that only the parameters which are common for all the interfaces (e.g. ID, status) can be printed out because IP address is not configured on all the interfaces. Trying to find an item in the json response not correctly
copied the important lines of python app: - get_interface_response = requests.get(get_interface_url, verify=False)
get_interface_json = get_interface_response.json()
interfaces = get_interface_json["response"]
with open("test.txt", "w") as file:
for item in interfaces:
file.write ("ID = " + item["id"] + "\n\n\n" + " IP address= " + "\n\n" + item["ipv4Address"] + "\n\n") <-- once I change ipv4Address to other common item like "status", everything is printed out well.
Tested similar code with 1.0.1.30, and it works fine. Even interfaces without a v4 address have the key ipv4Address. The value is None, so you have to make sure you cast to a string. I'm including my code here ==> [marcuscom] Log of /py-apic-em/py-apic-em.py.
Note: Use the text return from the requests interface. Perhaps the JSON method is trimming those keys with None values (though that would be odd).
Use following code if you filter on interfaces with IP address configured, the code is the following:
with open(filename, "w") as file:
for item in interfaces:
if "ipv4Address" in item:
file.write ("ID = " + item["id"] + " IP address and mask = " + item["ipv4Address"] + " /" + item["ipv4Mask"] + "\n")
Comments
0 comments
Please sign in to leave a comment.