cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
876
Views
0
Helpful
2
Replies

KeyError: 'ipv4Mask'

Hi All,

I am developing an app for IP address management. I have the folowing error when I am 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. Does my thesis correct or I am trying to find an item in the json response not correctly?

I copied the important lines of my 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.

What can be the issue with it?

1 Accepted Solution

Accepted Solutions

Joe Clarke
Cisco Employee
Cisco Employee

What version of APIC-EM are you using?  I 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: I use the text return from the requests interface.  Perhaps the JSON method is trimming those keys with None values (though that would be odd).

View solution in original post

2 Replies 2

Joe Clarke
Cisco Employee
Cisco Employee

What version of APIC-EM are you using?  I 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: I use the text return from the requests interface.  Perhaps the JSON method is trimming those keys with None values (though that would be odd).

Thank you. I found out that if I 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")

And first time I used -->  if "ipv4Address" in interfaces:

It was my mistake but thanks for your prompt answer.

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:


This community is intended for developer topics around Data Center technology and products. If you are looking for a non-developer topic about Data Center, you might find additional information in the Data Center and Cloud community