So we’ve looked at running CLI commands using python, and you might be like “so what.” (If you haven’t been following along you may need to catch up some) Yes there is some benefit in automating logging into a controller, running some commands, and saving the response to a file. Maybe even running the script before and after changes. That could save some time write change controls and running validations. But what I want to show you is how to get that response in python and turn it into a useful dictionary.
TextFSM was created by Google and is installed as part of Netmiko! So if you are using Netmiko like I showed in my previous post, you can start leveraging this right away. The only caveat is there are not many templates for wireless devices in the current repository. Any device that works with Netmiko could work with TextFSM but templates might need to be built. I have built quite a few templates for cisco aironet and will share my github repo. I have submitted them to be included in TextFSM but I am waiting for a review.
You can get the repo for TextFSM here.
https://github.com/networktocode/ntc-templates
The commands included are:
show cdp neighbors detail
show ap config general
show ap summary
show sysinfo
My copy of the repo is here.
https://github.com/timjsmith24/ntc-templates-with-ciscowlc.git
The commands I have added are the following:
show 802.11a cleanair config
show 802.11b cleanair config
show network assurance summary
show mdns service summary
show advanced probe
show load-balancing
show interface summary
show inventory
show wlan apgroups
show advanced fra
show band-select
show network summary
show trapflags
show sessions
show wlan summary
show 802.11a
show 802.11b
show time
So for now I will go through adding my version with the additional cisco aironet templates. To run the cli command and parsing the results is actually a really easy process.
By default, Netmiko is going to look look for an index file for the templates in the specific folder /Users/<user name>/ntc-templates/templates. So if you would like to install the repo there, in the terminal window type ‘cd ~’ this will take you to your root directory. Then type git clone https://github.com/timjsmith24/ntc-templates.git.
Once you have it installed you will need to fix a couple things in the index file. Here is a link to instructions on how to do this will an explanation as to why. https://bmatwifilabs.wordpress.com/2019/09/21/issues-with-textfsm-index-file/
Once that is fixed you are ready to go.
All you need to do to take advantage of this is add ‘use_textfsm=True’ after the command when sending with Netmiko. As long as a template exists and the index file is correct the result will be a dictionary!
output = netconnect.send_command(cmd, use_textfsm=True)
So lets look at an index file.
If you open the cisco_wlc_ssh_show_inventory.template file (using sublime) you will see at the top a bunch of Values
Value MAC (([\d1-9A-F]{2}\:?){6})
Value MAX_AP_COUNT (\d+)
Value TYPE (\w+)
Value DESCR (.+?)
Value PID (\w+\-\S+\-\S+)
Value VID (\S+)
Value SERIAL_NUMBER (\S+)
The strings after ‘Value’ are the variables defined for the template. These will be the keys of the dictionary. Following that in the ‘()’ are the regex the template will be looking for in the CLI response.
After that you will see a line with ‘Start’ and then the lines under that with a 2 space indent followed by a ^. These lines are regex of a line in the CLI response.
Start
^Burned\-in\sMAC\sAddress\.*\s${MAC}\s*$$
^Maximum number of APs supported\.*\s${MAX_AP_COUNT}\s*$$
^NAME:\s\”${TYPE}\”\s+,\sDESCR:\s${DESCR}\s*$$
^PID:\s${PID},\s+VID:\s${VID},\s+SN:\s${SERIAL_NUMBER}
What happens is the template will go through the response and look for any line that matches the first regex line. Once it finds one it will look for a line that matches the second regex line and so forth.
Ok so let’s go through the first line. It will look for the actual characters ‘Burned’ the the ‘\-‘ says to look for an actual ‘-‘ character, the ‘\s’ says to look for a single space. the ‘\.*’ means to look for 0 or more periods (.)s. So there could be 0 ‘.’, 1′.’ or 20 ‘.’s. The ${MAC} will look for the regex defined for MAC Value. ‘\s*$$‘ means 0 or more spaces followed by an end of line. If a line is found with that entire regex then MAC will become the part of the regex found with ${MAC}.
Building your own templates can take a little practice and time, but once you get familiar with regex you can build them pretty fast. There are ways to build lists and search for identical lines and other features. If you look at the README file in the /Users/<user name>/ntc-templates/ there are more instructions there. Including how to name the files, update the index file and more.