So we gonna discuss how you can get the desired text in JSON format from any Cisco devices(routers, switches, Data Centers etc). Before Starting If you are reading this I assume you are on way of becoming a Network Engineer and also you may have some basic knowledge of using python.
Here is sample command output of show environment from Sample router
So we would take the above command output Text as input and write an TextFSM Logic to get the output as we required. The requirement will differ from individual to individual. For ex. some developer may require only sensor details and some may only require Threshold.
Before Starting you may have to Install Textfsm module. If you have installed netmiko module Textfsm already included in that or else just run the following command in terminal and TextFSM will be imported.
pip install textfsm
If you are using Visual Studio code its better to download an Extension for textfsm which will make everything colorful ;).
Its not that difficult to understand. In line 1 SLOT includes \w\d i.e \w means any one letter and \d means one numerical value. So if you compare the command output of show environment in SLOT coloum it includes value like P0, R1 etc. Similarly for nth number of numerical value use \d+ similarly with \w+. To understand better I would prefer first learn the meaning of each character from Regular Expression Quickstart .
Now its time to write to Python script which takes textfsm and Command Output as input and gives you the required JSON output.
The output is structured json format which is generally used by developer to perform the necessary operation.
Most of them will think that task is done, since we are getting the desired output what we had excepted. But what I have experienced and what Industries normally follow is testing of all this templates. Testing can be done by comparing the output and Command output to check whether it includes all values. But as a Engineer we have to do something special you can include your logic for comparing and Testing.
What I did was I wrote a Python Script which can give the Accuracy of each template. If the Accuracy is below the 90% I would check whether templates is wrong or command output is missing. Usually my templates came out with 100% Accuracy ;)) xd.. For more information you can visit to TextFSM Module.
Thank for Reading If you have any doubts feel free to connect