Summary
Question
Introduction – Why Python 3
Python is one of the most popular scripting languages used nowadays (Lutz, 2013):
· Software quality: Python code was designed readable hence reusable and maintainable.
· Developer productivity: compared to the compiled languages, Python is easier to debug and maintain.
· Program portability: Python is available in the major computer platforms.
· Support libraries: it comes with a large collection of prebuilt libraries (standard libraries). Around internet there is a vast collection of third party libraries.
· Component integration: python supports a variety of integration mechanism. So that it can work with native libraries, Java, .NET and other frameworks; thanks to protocols like SOAP, XML-RPC and CORBA, it can interact with Web applications.
· Enjoyment: there is an intangible advantage when it is easy to get the things done.
The more popular uses for Python are: system programming, GUIs, Internet scripting, component integration, rapid prototyping, numeric and scientific programming, games, data mining, etc…
Python 3 includes a lot of improvements in the language which break the compatibility with Python 2, between the new features includes (ActiveState, 2017):
· The integer division which is much simpler and stronger in Python 3.
· Asynchronous support.
· Better exception handling.
· Unicode support.
· Python 3 can mix tabs and spaces.
· Function annotations.
· Memory objects which are significantly more memory efficient.
How to create Python3 scripts for Microfocus Network Automation
Requirements:
· Python 3 (examples shown in this document were tested with miniconda).
· The library for SOAP communications called “suds-jurko”.
· The binding.
Miniconda
It’s an easy to install Python environment available for Windows, MacOS and Linux (https://conda.io/miniconda.html).
It installs a Python instance independently to the one installed in the system.
Installation steps:
· Install Miniconda3
· Install Suds by Jurkos:
./conda install suds-jurko
· Copy the binding in a convenient location.
Examples:
Disclaimer this example is used to illustrate the workaround it was provided with no warranties.
1. List devices in the inventory:
deviceGroup = "Inventory" from SOAPbinding import NetworkAutomationSOAPConnection soappyFile = 'SOAPbinding/hpna10_30_api.wsdl.soappy' myserver, myuser, mypassword = 'http://10.10.70.2', 'myuser', 'mypassword' na = NetworkAutomationSOAPConnection(myserver, myuser, mypassword, soappyFile) ld_result = na.execute('list_device', {'group':deviceGroup}) print(ld_result) |
(Result){ Status = "501" ResultSet = (ResultSet){ Row[] = (Row){ accessMethods = "TFTP:SFTP:FTP:HTTPS:HTTP:SCP:SNMP:CLI+commstr:ssh:telnet" assetTag = None changeEventData = "<event>201,command script execution,2018:01:05:11:14:45,"",null</event> <event>201,command script execution,2018:01:05:11:39:04,"",null</event> " comments = None createDate = "Fri Jan 05 11:11:59 CST 2018" deviceGroup1ID = 12 deviceID = 201 deviceName = None driverName = "Avocent" excludeFromPoll = 0 feedSource = "Manually added by User-1deviceAtTime" forceSave = "false" geographicalLocation = None hostName = "10.10.10.10" lastAccessAttemptDate = "Tue Feb 06 09:53:33 CST 2018" lastAccessAttemptStatus = "Take Snapshot: Problem accessing device" lastModifiedUserID = 1 lastRecordModifiedDate = "Tue Feb 06 09:53:34 CST 2018" lastSnapshotAttemptDate = "Tue Feb 06 09:53:33 CST 2018" lastSnapshotAttemptStatus = "Problem accessing device" managementStatus = 0 model = None performACLParsing = 2 primaryFQDN = None primaryIPAddress = "10.10.10.10" serialNumber = None siteID = 1 siteName = "Default Site" vendor = None }, } } Done! |
Some examples can be downloaded HERE. The pack also includes the binding and several api.wsdl.soappy files.
How to use Python 3 for advanced scripting:
1. Set Python 3 like interpreter in:
General menu --> Admin > Administrative settings > Server
Advanced Scripting
2. Create advanced scripts that starts with:
BINDING_PATH = "HERE THE BINDING PATH" SOAPPYFILE = "HERE_THE_NA_PATH/client/sdk/api.wsdl.soappy" import sys sys.path.insert(0, BINDING_PATH) from SOAPbinding import NetworkAutomationSOAPConnection from credentials import * na = NetworkAutomationSOAPConnection("http://localhost", "$tc_user_username$", "$tc_user_password$", SOAPPYFILE) |
For example:
Interpreter configuration:
Script:
BINDING_PATH = "/opt/Python/API_Python/" SOAPPYFILE = "/opt/NA/client/sdk/api.wsdl.soappy" import sys sys.path.insert(0, BINDING_PATH) from SOAPbinding import NetworkAutomationSOAPConnection from credentials import * na = NetworkAutomationSOAPConnection("http://localhost", "$tc_user_username$", "$tc_user_password$", SOAPPYFILE) results = na.execute('show_latest_access', {'ip': '$tc_device_ip$'}) print(results) print("Done!") |
Result:
Known problems:
Problem: SUDS creates many temporal files.
Solution:
https://bitbucket.org/jurko/suds/issues/15/insecure-temporary-directory-use
Problem: Complex type not found. The scripts dies and left this error message:
raise TypeNotFound(content.node.qname()) suds.TypeNotFound: Type not found: 'nas:_newDeviceInImport' |
Solution:
Go to <NA_HOME>/jre/appserver.rcx (first backup):
Locate the block: <array name="api/soap_filter_columns/10/g">
Rename it to: <array name="api/soap_filter_columns/10/10">
Make sure that this line is not present: <value>_newDeviceInImport</value>
Make sure that this line is present: <value>__newDeviceInImport</value>
Save.
Reload the server.
HP Network Automation Version 10.11.05 Type "HELP connect" to see how to connect to a device. Type "HELP" to view a list of available commands. NA>reload server options 200 OK: Generic Succeeded Removed: api/soap_filter_columns/10/g=[__m_extraObjects, __loadedSecondaryIPs, __columnUpdateMap, __columnUpdateSet, __newDeviceInImport, __changedInCache, localizedComments, localizedDeviceGroupName, durationStr] Added: api/soap_filter_columns/10/10=[__m_extraObjects, __loadedSecondaryIPs, __columnUpdateMap, __columnUpdateSet, __newDeviceInImport, __changedInCache, localizedComments, localizedDeviceGroupName, durationStr] COMMAND COMPLETED NA> |
Bibliography:
Lutz, M. (2013). Learning Python. Sebastopol, CA: O'Reilly.
ActiveState. (2017). Python 3 vs Python 2: It’s Different This Time. [online] Available at: https://www.activestate.com/blog/2017/01/python-3-vs-python-2-its-different-time [Accessed 6 Feb. 2018].
I hope you find this information useful.
Saúl Vega Chacón.
Answer
Python 3 for scripting in Microfocus Network Automation