def test_clitable_to_dict(): """Converts TextFSM cli_table object to list of dictionaries""" table = clitable.CliTable(template_dir=RESOURCE_FOLDER) text_filename = join(RESOURCE_FOLDER, "textfsm.txt") template_filename = join(RESOURCE_FOLDER, "cisco_ios_show_version.template") with open(text_filename) as data_file: text = data_file.read() with open(template_filename) as template_file: table = table._ParseCmdItem(text, template_file) result = utilities.clitable_to_dict(table) assert result == [{"model": "4500"}]
from netmiko.utilities import clitable_to_dict from textfsm import clitable index_file = 'index' template_dir = '/Users/thanadet.k/Projects/ntc-templates/ntc_templates/templates' raw_text = open('show_interface.log', mode='r', encoding='utf-8').read() cli_table = clitable.CliTable(index_file, template_dir) attrs = {'Command': 'show interface', 'platform': 'cisco_ios'} cli_table.ParseCmd(raw_text, attrs) structured_data = clitable_to_dict(cli_table) print(structured_data)