Help with parsing a dict from Vendor's API?

Peter Otten __peter__ at web.de
Wed Oct 15 11:22:26 CEST 2014


Nick Ellson wrote:

> Hello!
> 
> I have a very specific question related to the output of a Vendors API
> (Palo Alto Networks "pan.xapi" and how I might farm data from this output.
> I am new to python, doing well in the tutorials, but this is an automation
> task at work and I know the rest will be much easier once i get past the
> ability to read this dict.
> 
> The code reaches in to the central Palo Alto firewall manager (Panorama)
> and executes a simple command to return all of the information for each of
> the managed firewalls in the field. It captured this output in XML I
> believe, but has the ability to return it in python dict format too, which
> looked like probably the best format to use. Here is the test I tried
> 
> <snip>
> xapi.op(cmd='show devices connected', cmd_xml=True )
> MyDict=xapi.xml_python()
> print (type(MyDict))
> print (MyDict)
> <snip>
> 
> and I get: (This displays only 2 firewalls of the 180, so you can see the
> structure, and that python does say it is a "dict")
> 
> bertha bin # ./test.py
> <class 'dict'>
> {'response': {'result': {'devices': {'entry': [{'av-version': '1391-1863',
> 'unsupported-version': False, 'ip-address': '1.8.2.8', 'sw-version':
> '4.1.9', 'vsys': {'entry': [{'name': 'vsys1', 'shared-policy-md5sum':
> '8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None,
> 'display-name': 'vsys1'}]}, 'uptime': '350 days, 14:29:49',
> 'threat-version': '460-2394', 'operational-mode': 'normal', 'multi-vsys':
> False, 'global-protect-client-package-version': '0.0.0', 'app-version':
> '460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000002',
> 'family': '200', 'url-filtering-version': '4390', 'vpn-disable-mode':
> False, 'logdb-version': '4.1.2', 'serial': '001606000002', 'hostname':
> 'bob-int-fw'}, {'av-version': '1391-1863', 'unsupported-version': False,
> 'ip-address': '1.9.8.8', 'sw-version': '4.1.9', 'vsys': {'entry':
> [{'name': 'vsys1', 'shared-policy-md5sum':
> '8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None,
> 'display-name': 'vsys1'}]}, 'uptime': '358 days, 0:03:20',
> 'threat-version': '460-2394', 'operational-mode': 'normal', 'multi-vsys':
> False, 'global-protect-client-package-version': '0.0.0', 'app-version':
> '460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000009',
> 'family': '200', 'url-filtering-version': '4390', 'vpn-disable-mode':
> False, 'logdb-version': '4.1.2', 'serial': '001606008639', 'hostname':
> 'bib-int-fw'}, <****repeats for 180 firewalls****> ]}}, 'status':
> 'success'}}
> 
> 
> What I want is to parse through each firewall grabbing the "ip-address"
> value so that I can dump it to a list:
> 
> <ip>
> <ip>
> <ip>
> 
> For use in another network management tool so I don't rely on outsourced
> help to remember to place teh firewalls into the correct tools.
> 
> But dang if every dict tutorial seems to deal with slightly simpler
> looking structures than what this puts out. I would be very appreciative
> with help stepping out of the 6 line "address book/grocery list" example
> world for a taste of something useful :-)
> 
> Maybe to a Python coder, it maybe a simple even be able to randomly
> reference a firewall index number and teh value in this structure so one
> can easily just pluck any A/V pair at will.. just not for me yet :-D

Look at your dict (I use the name 'd' instead of 'MyDict'):

d = {"response": {"result": ...}

So

d["response"]

will give you 

{"result": {"devices": ...}

A good tool to explore a data structure like this is the interactive 
interpreter. If you invoke the script with

python -i test.py

you have all its global variables available, i. e.

>>> MyDict
{ ... } # you should see the dictionary contents


On we go:

d["response"]["result"]

will give

{"devices": {"entry": ...}

Next step:

d["response"]["result"]["devices"]

-->

{"entry": [...]

That's a list for change, so you loop over it:

entries = d["response"]["result"]["devices"]["entry"]
for entry in entries:
    print(entry) # --> {..., 'ip-address': '1.8.2.8', ... }

So the final code is:

for entry in d["response"]["result"]["devices"]["entry"]:
    print(entry["ip-address"])





More information about the Python-list mailing list