I have used TextFSM in my Netmiko TACACS script in order to parse the show version and show inventory outputs to determine the type of Cisco OS the device is.
The templates maybe found https://github.com/networktocode/ntc-templates/tree/master/ntc_templates/templates
They were already installed when I installed Netmiko
0 1 2 3 |
$ pip freeze | grep ntc ntc-templates==3.1.0 |
Using TextFSM is very simple. Netmiko takes in one extra parameter to use TextFSM which is True or False. An example from my Revision2 TACACS script.
0 1 2 |
show_version = BaseConn.send_commands(self, connection, "show version", True) |
0 1 2 3 4 |
def send_commands(self,connection, command, use_textfsm): show_tacacs = connection.send_command(command, use_textfsm=use_textfsm) return show_tacacs |
Examples of the dictionaries that are returned when parsed. This is an NXOS virtual switch running in my GNS3 lab.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#Show Version [ { "version": "", "rommon": "", "hostname": "Kernel", "uptime": "3 day(s), 16 hour(s), 57 minute(s), 16 second(s)", "uptime_years": "", "uptime_weeks": "", "uptime_days": "3", "uptime_hours": "16", "uptime_minutes": "57", "reload_reason": "", "running_image": "", "hardware": [], "serial": [], "config_register": "", "mac": [], "restarted": "", } ] |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#Show Inventory [ { "name": "Chassis", "descr": "NX-OSv Chassis ", "pid": "N7K-C7018", "vid": "V00 ", "sn": "TBF636E000B", }, { "name": "Slot 1", "descr": "NX-OSv Supervisor Module", "pid": "N7K-SUP1", "vid": "V00 ", "sn": "TMF636E000B", }, { "name": "Slot 2", "descr": "NX-OSv Ethernet Module", "pid": "N7K-F248XP-25", "vid": "V00 ", "sn": "TMF636E000C", }, { "name": "Slot 3", "descr": "NX-OSv Ethernet Module", "pid": "N7K-F248XP-25", "vid": "V00 ", "sn": "TMF636E000D", }, { "name": "Slot 4", "descr": "NX-OSv Ethernet Module", "pid": "N7K-F248XP-25", "vid": "V00 ", "sn": "TMF636E000E", }, { "name": "Slot 33", "descr": "NX-OSv Chassis Power Supply", "pid": "", "vid": "V00 ", "sn": "", }, { "name": "Slot 35", "descr": "NX-OSv Chassis Fan Module", "pid": "", "vid": "V00 ", "sn": "", }, ] |