带有Netmiko的嵌套字典

时间:2019-09-25 21:23:53

标签: python automation

我最初的问题已得到回答,但现在我面临另一个问题。

Get index name of a list made from dictionaries

我试图从列表中获取字典的名称,并在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:~$

我试图弄清楚为什么它会跳过其他条目。有想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

循环如下:

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中的最后一个项目(因为这是我们完成循环的项目)。

尝试简单地缩进所有内容,

(即

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)

等)

看看会发生什么!