python - Netmiko 嵌套字典
问题描述
我最初的问题得到了回答,但我现在面临另一个问题。
我试图从列表中获取字典的名称并在for
循环中使用它,但似乎无法完成。嵌套字典是我的问题的解决方案。
现在我可以使用 Netmiko 成功地向网络设备发送命令,但它似乎只使用字典中的最后一个条目。这是代码:
from netmiko import ConnectHandler
site1_switches = {
'visw0102' : {
'device_type': 'hp_comware',
'ip': '192.168.0.241',
'username': 'admin',
'password': 'password'
},
'visw0103' : {
'device_type': 'hp_comware',
'ip': '192.168.0.242',
'username': 'admin',
'password': 'password'
},
'visw0105' : {
'device_type': 'hp_comware',
'ip': '192.168.0.244',
'username': 'admin',
'password': 'password'
}
}
vlans = {
'300': 'TEST1',
'310': 'TEST2',
'320': 'TEST3',
'330': 'TEST4',
'340': 'TEST5'
}
for key, values in site1_switches.items():
device_type = values.get('device_type', {})
ip_address = values.get('ip', {})
username = values.get('username', {})
password = values.get('password', {})
net_connect = ConnectHandler(device_type=device_type, host=ip_address, username=username, password=password)
output = net_connect.send_command_timing(
'y',
strip_prompt=False,
strip_command=False
)
output = net_connect.send_command_timing(
'_cmdline-mode on',
strip_prompt=False,
strip_command=False
)
print (output)
if 'Continue' in output:
output += net_connect.send_command_timing(
'y',
strip_prompt=False,
strip_command=False
)
print (output)
if 'ssword' in output:
net_connect.send_command_timing(
'512900',
strip_prompt=False,
strip_command=False
)
print (output)
output = net_connect.send_command_timing(
'system-view',
strip_prompt=False,
strip_command=False
)
print (output)
for tag, vlan_name in vlans.items():
output = net_connect.send_command_timing(
'vlan' + ' ' + tag,
strip_prompt=False,
strip_command=False
)
print (output)
output = net_connect.send_command_timing(
'description' + ' ' + vlan_name,
strip_prompt=False,
strip_command=False
)
print (output)
命令运行成功,但仅针对嵌套字典 (VISW0105) 中的最后一个条目。这是输出:
administrator@vimgmt0103:~$ python3 test_netmiko.py
_cmdline-mode on
All commands can be displayed and executed. Continue? [Y/N]
_cmdline-mode on
All commands can be displayed and executed. Continue? [Y/N]y
Please input password:
_cmdline-mode on
All commands can be displayed and executed. Continue? [Y/N]y
Please input password:
system-view
System View: return to User View with Ctrl+Z.
[VISW0105] <-- This is the last entry (switch) in the dictionary
vlan 300
[VISW0105-vlan300]
description TEST1
[VISW0105-vlan300]
vlan 310
[VISW0105-vlan310]
description TEST2
[VISW0105-vlan310]
vlan 320
[VISW0105-vlan320]
description TEST3
[VISW0105-vlan320]
vlan 330
[VISW0105-vlan330]
description TEST4
[VISW0105-vlan330]
vlan 340
[VISW0105-vlan340]
description TEST5
[VISW0105-vlan340]
administrator@vimgmt0103:~$
我试图弄清楚为什么它会跳过其他条目。任何的想法?
谢谢!
解决方案
循环如下:
for key, values in site1_switches.items():
device_type = values.get('device_type', {})
ip_address = values.get('ip', {})
username = values.get('username', {})
password = values.get('password', {})
之后的所有内容都不在循环中,因为您取消了缩进。
所以从循环结束的那一刻起,一切都指向最后一个项目site1_switches
(因为那是我们完成循环的项目)。
尝试简单地缩进下面的所有内容,
(IE
for key, values in site1_switches.items():
device_type = values.get('device_type', {})
ip_address = values.get('ip', {})
username = values.get('username', {})
password = values.get('password', {})
net_connect = ConnectHandler(device_type=device_type, host=ip_address, username=username, password=password)
ETC。)
看看会发生什么!
推荐阅读
- django - 有没有办法我可以加入两个表并获得 Django 中重复字段的总和
- python - 从重叠的 matplotlib 子图中删除白色背景
- python - 无法从 Axios POST 访问表单数据到 Flask
- python - 是否可以更改 Python 中内置类的 dunder 方法?
- android-studio - 确保我不会将密码推送到 GitHub 以进行 Flutter 开发
- sql - Oracle中SQL查询的最大长度
- r - 列表对象(数据框)的下标超出范围,大小和格式相同
- php - PHP- FETCHALL 由于返回大结果而失败
- reactjs - TypeError:未定义不是一个对象(评估'_firebase.firebase.database')
- flutter - 显示从设备颤振上传的文本文件的内容