I have the following code to connect to the switch and run two commands and save them as CSV, but the issue is when I run it, it will create two files with a same result! both are the result of "Show Vlan" It's like it replaces the first command. Any idea how to fix?
def device2(whid, idf, username, password):
checkOnline = {
"device_type": "cisco_ios",
"host": ("switch1-1"),
"username": username,
"password": password,
}
command1 = "show interfaces description"
command2 = "show vlan"
with ConnectHandler(**checkOnline) as net_connect:
out1 = net_connect.send_command(command1)
out2 = net_connect.send_command(command2)
def cdp():
print(out1)
def cdp():
print(out2)
raw_text_data1 = out1
raw_text_data2 = out2
template1= open("/var/assets/showDesc.textfsm")
template2 = open("/var/assets/showVlan.textfsm")
re_table = textfsm.TextFSM(template1)
re_table = textfsm.TextFSM(template2)
fsm_results1 = re_table.ParseText(raw_text_data1)
fsm_results2 = re_table.ParseText(raw_text_data2)
outfile_name1 = open("/var/assets/file1.csv", "w ")
outfile1 = outfile_name1
for s in re_table.header:
outfile1.write("%s," % s)
outfile1.write("\n")
counter = 0
for row in fsm_results1:
for s in row:
outfile1.write("%s," % s)
outfile1.write("\n")
counter = 1
outfile1.close()
outfile_name2 = open("/var/assets/file2.csv", "w ")
outfile2 = outfile_name2
for s in re_table.header:
outfile2.write("%s," % s)
outfile2.write("\n")
counter = 0
for row in fsm_results2:
for s in row:
outfile2.write("%s," % s)
outfile2.write("\n")
counter = 1
outfile2.close()
CodePudding user response:
You've assigned both textfsm.TextFSM(template1)
and textfsm.TextFSM(template2)
into one variable re_table
.
I assume, you wanted to assign them to different variables, such as re_table1
and re_table2