Thursday, April 25, 2019

TextFSM and Netmiko outputing to CSV file

Hey guys,

First bear in mind as I'm still learning python and by no means I'm in my confort zone, but I'm hoping to learn and move forward.

I've decide to work on some of need by having a script that perform various check, one of them is to report the "show cdp neigh detail" and document the IP + MAC + type of device connected to it ( mainly AP, running on Aruba and HP devices) I've got my TextFSM template working

I already have a python script that would read the output on a file and run TextFSM on it and return a CSV file, pretty handy however I'm aiming to get a all in one script and I'm aiming to eliminate the need of parsing the output of the switch on a .txt file

so far I have this def () created, however I'm unable to get it working so far

def cdp(): out = conn.send_command("show cdp neighbors detail", use_textfsm=True) print(out) input_file = out raw_text_data = input_file template = open("/root/ntc-templates/templates/hp_procurve_show_cdp_multiple.template") re_table = textfsm.TextFSM(template) fsm_results = re_table.ParseText(raw_text_data) outfile_name = open("outfile.csv", "w+") outfile = outfile_name print(re_table.header) for s in re_table.header: outfile.write("%s;" % s) outfile.write("\n") counter = 0 for row in fsm_results: print(row) for s in row: outfile.write("%s;" % s) outfile.write("\n") counter += 1 print("Write %d records" % counter) 

but the script return:

[{'plateform': 'Cisco IOS', 'hostname': '', 'ipaddress': '172.27.254.210', 'port': '48', 'address': ''}, {'plateform': 'Cisco IOS', 'hostname': '', 'ipaddress': '172.27.254.210', 'port': '48', 'address': '24 e9 b3 a0 c8 80'}] [{'plateform': 'Cisco IOS', 'hostname': '', 'ipaddress': '172.27.254.210', 'port': '48', 'address': ''}, {'plateform': 'Cisco IOS', 'hostname': '', 'ipaddress': '172.27.254.210', 'port': '48', 'address': '24 e9 b3 a0 c8 80'}] Traceback (most recent call last): File "netpy.py", line 209, in <module> cdp() File "netpy.py", line 80, in cdp fsm_results = re_table.ParseText(raw_text_data) File "/usr/local/lib/python2.7/site-packages/textfsm.py", line 882, in ParseText lines = text.splitlines() AttributeError: 'list' object has no attribute 'splitlines' 

Notice at the top netmiko return the text formated using my TextFSM template but fail on the fsm_results it seems

not sure if anyone could help me figuring out what could be wrong

thanks guys



No comments:

Post a Comment