paramiko-expect with Python 2-3 'str' buffer interface issues?
Hello, Newly trying out programming, Python for Network Engineers, and loving it. So I found paramiko for SSH, and now paramiko-expect for ssh expect like behavior. I am using Python 3.4 and have been through a number of examples of changing code to work in the 3.x environment. But is anyone using paramiko-expect? It seems to have the usual 'str' type issue I have seen, and used var.decode() for, but can't seem to make it happy with this module. Nick Traceback (most recent call last): File ./ssh-expect.py, line 29, in module interact.expect(prompt) File /usr/lib64/python3.4/site-packages/paramikoe.py, line 130, in expect buffer = buffer.replace('\r', '') TypeError: 'str' does not support the buffer interface #!/usr/bin/env python # PyNet Class Exercises by Nick Ellson __author__ = Nick Ellson import paramiko from paramikoe import SSHClientInteraction if __name__ == '__main__': ip = '10.10.10.10' username = 'user' password = 'password' prompt = 'router#' remote_conn_pre = paramiko.SSHClient() remote_conn_pre.set_missing_host_key_policy( paramiko.AutoAddPolicy()) remote_conn_pre.connect(ip, username=username, password=password, allow_agent=False, look_for_keys=False) print (SSH connection established to %s % ip) interact = SSHClientInteraction(remote_conn_pre, timeout=10, display=True) interact.expect(prompt) interact.send('terminal length 0') interact.expect(prompt) interact.send('show version') interact.expect(prompt) cmd_output = interact.current_output_clean print (cmd_output) remote_conn_pre.close() -- https://mail.python.org/mailman/listinfo/python-list
Help with parsing a dict from Vendor's API?
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': '00160602', 'family': '200', 'url-filtering-version': '4390', 'vpn-disable-mode': False, 'logdb-version': '4.1.2', 'serial': '00160602', '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': '00160609', '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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with parsing a dict from Vendor's API?
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. On Oct 15, 2014, at 2:22 AM, Peter Otten __pete...@web.de wrote: 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': '00160602', 'family': '200', 'url-filtering-version': '4390', 'vpn-disable-mode': False, 'logdb-version': '4.1.2', 'serial': '00160602', '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': '00160609', '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]) -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list