在 运行 两个命令上获得相同的结果 (Netmiko)
Getting same result on running two commands (Netmiko)
我有以下代码连接到交换机和 运行 两个命令并将它们保存为 CSV,但问题是当我 运行 它时,它会创建两个具有相同的文件结果!两者都是“Show Vlan”的结果
这就像它取代了第一个命令。知道如何解决吗?
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()
您已将 textfsm.TextFSM(template1)
和 textfsm.TextFSM(template2)
分配给一个变量 re_table
。
我假设,您想将它们分配给不同的变量,例如 re_table1
和 re_table2
在这一行中,table 对象具有相同的名称:
re_table = textfsm.TextFSM(template1)
re_table = textfsm.TextFSM(template2)
请检查它们
我有以下代码连接到交换机和 运行 两个命令并将它们保存为 CSV,但问题是当我 运行 它时,它会创建两个具有相同的文件结果!两者都是“Show Vlan”的结果 这就像它取代了第一个命令。知道如何解决吗?
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()
您已将 textfsm.TextFSM(template1)
和 textfsm.TextFSM(template2)
分配给一个变量 re_table
。
我假设,您想将它们分配给不同的变量,例如 re_table1
和 re_table2
在这一行中,table 对象具有相同的名称:
re_table = textfsm.TextFSM(template1)
re_table = textfsm.TextFSM(template2)
请检查它们