06-17-2020 06:34 AM
Hi there
I'm trying to create a script after relentless hours of study :-)
I just need to know how to iterate through the xml data and 'print' out the requested IPV4 address
This is the script:
[all information is more then welcome: the 'output print to a txt' file is here not finished.] just got stuck in the basics
Solved! Go to Solution.
06-18-2020 12:56 AM - edited 06-18-2020 01:24 AM
Hi
Whats the issue? Is it the key error?
Interface : GigabitEthernet1 : ip : 10.10.20.48 : netmask : 255.255.255.0 : Status : true : Interface : GigabitEthernet2 : ip : 10.255.255.1 : netmask : 255.255.255.0 : Status : true : Traceback (most recent call last): File "test.py", line 57, in <module> ip = interface["ipv4"]["address"]["ip"], KeyError: 'address'
If the issue is the key error - its because the interface doesn't have an address configured.
You can use a try-except block to catch the key error.
Something like this may work -
# Create a list of interfaces for interface in interfaces: try: print("Interface : {name} : ip : {ip} : netmask : {mask} : Status : {status} : ".format( name = interface["name"], ip = interface["ipv4"]["address"]["ip"], mask = interface["ipv4"]["address"]["netmask"], status = interface["enabled"] ) ) except KeyError: print("Interface : {name} : Status : {status} : ".format( name = interface["name"], status = interface["enabled"] ) )
06-18-2020 12:56 AM - edited 06-18-2020 01:24 AM
Hi
Whats the issue? Is it the key error?
Interface : GigabitEthernet1 : ip : 10.10.20.48 : netmask : 255.255.255.0 : Status : true : Interface : GigabitEthernet2 : ip : 10.255.255.1 : netmask : 255.255.255.0 : Status : true : Traceback (most recent call last): File "test.py", line 57, in <module> ip = interface["ipv4"]["address"]["ip"], KeyError: 'address'
If the issue is the key error - its because the interface doesn't have an address configured.
You can use a try-except block to catch the key error.
Something like this may work -
# Create a list of interfaces for interface in interfaces: try: print("Interface : {name} : ip : {ip} : netmask : {mask} : Status : {status} : ".format( name = interface["name"], ip = interface["ipv4"]["address"]["ip"], mask = interface["ipv4"]["address"]["netmask"], status = interface["enabled"] ) ) except KeyError: print("Interface : {name} : Status : {status} : ".format( name = interface["name"], status = interface["enabled"] ) )
06-18-2020 01:42 AM
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide