Discussion:
Help with parsing a dict from Vendor's API?
Nick Ellson
2014-10-14 20:21:14 UTC
Permalink
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

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141014/570a3bce/attachment.html>
Peter Otten
2014-10-15 09:22:26 UTC
Permalink
Post by Nick Ellson
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',
'8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None,
'display-name': 'vsys1'}]}, 'uptime': '350 days, 14:29:49',
'460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000002',
'bob-int-fw'}, {'av-version': '1391-1863', 'unsupported-version': False,
'8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None,
'display-name': 'vsys1'}]}, 'uptime': '358 days, 0:03:20',
'460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000009',
'success'}}
What I want is to parse through each firewall grabbing the "ip-address"
<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.
Post by Nick Ellson
Post by Nick Ellson
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"])
Nick Ellson
2014-10-15 13:35:57 UTC
Permalink
Thank you Peter!

That makes sense, and I did find "pprint" that dumped it out aligned so I could actually see the nested layers you are referring to.

That got me my IP's. :-) I'll play with this now and see if I can harvest something targeted.. Like list all device host names running code 4.1.9, or display the serial number of the device with hostname 'foo'

That should get me on my way to productive fun :-)

Nick

Nick Ellson - from iPhone (forgive typos)
CCIE #20018
Network Hobbyist
"Educating Layer 8, one user at a time."
Post by Peter Otten
Post by Nick Ellson
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',
'8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None,
'display-name': 'vsys1'}]}, 'uptime': '350 days, 14:29:49',
'460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000002',
'bob-int-fw'}, {'av-version': '1391-1863', 'unsupported-version': False,
'8a8dcd146e24bd750ae571059bc09210', 'shared-policy-status': None,
'display-name': 'vsys1'}]}, 'uptime': '358 days, 0:03:20',
'460-2394', 'model': 'PA-200', 'connected': True, 'name': '001606000009',
'success'}}
What I want is to parse through each firewall grabbing the "ip-address"
<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
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.
Post by Nick Ellson
Post by Nick Ellson
MyDict
{ ... } # you should see the dictionary contents
d["response"]["result"]
will give
{"devices": {"entry": ...}
d["response"]["result"]["devices"]
-->
{"entry": [...]
entries = d["response"]["result"]["devices"]["entry"]
print(entry) # --> {..., 'ip-address': '1.8.2.8', ... }
print(entry["ip-address"])
--
https://mail.python.org/mailman/listinfo/python-list
Continue reading on narkive:
Loading...