![](/img/trans.png)
[英]How to save multiple outputs from a for loop in python to a text file?
[英]how to take multiple outputs from a loop to outside in python(netmiko)
我是 python 的新手。我有使用 .netmiko 在多個交換機上運行多個“顯示命令”的代碼,當一切都在循環中時它工作正常。 但是當我想通過將其分配為變量並打印它來將循環外的多個“顯示命令”中的這個 output 取出時,只打印輸出之一。
S1 = {
'device_type': 'cisco_ios',
'ip': '192.168.0.56',
'username': 'admin',
'password': 'admin'
}
S2= {
'device_type': 'cisco_ios',
'ip': '192.168.0.57',
'username': 'admin',
'password': 'admin'
}
all_devices = [S1,S2]
for devices in all_devices:
print("\nLogging into the switch...")
net_connect = ConnectHandler(**devices)
net_connect.enable()
cmd = ["show vlan brief", "\n","\n","show ip interface brief"]
for show in cmd:
output=net_connect.send_command(show)
y = output
print(y)
試試這個:
S1 = {
'device_type': 'cisco_ios',
'ip': '192.168.0.56',
'username': 'admin',
'password': 'admin'
}
S2= {
'device_type': 'cisco_ios',
'ip': '192.168.0.57',
'username': 'admin',
'password': 'admin'
}
all_devices = [S1,S2]
y = []
for devices in all_devices:
print("\nLogging into the switch...")
net_connect = ConnectHandler(**devices)
net_connect.enable()
cmd = ["show vlan brief", "\n","\n","show ip interface brief"]
for show in cmd:
output=net_connect.send_command(show)
y.append(output)
for x in y:
print(x)
我是 python 的新手。 我有代碼可以使用 netmiko 在多個交換機上運行多個“顯示命令”,當一切都在循環中時,它工作正常。 但是,當我想通過將其分配為變量並打印它來獲取循環外多個“顯示命令”的 output 時,只打印其中一個輸出。
S1 = {
'device_type': 'cisco_ios',
'ip': '192.168.0.56',
'username': 'admin',
'password': 'admin'
}
S2= {
'device_type': 'cisco_ios',
'ip': '192.168.0.57',
'username': 'admin',
'password': 'admin'
}
all_devices = [S1,S2]
for devices in all_devices:
print("\nLogging into the switch...")
net_connect = ConnectHandler(**devices)
net_connect.enable()
cmd = ["show vlan brief", "\n","\n","show ip interface brief"]
for show in cmd:
output=net_connect.send_command(show)
y = output
print(y)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.