带有Netmiko的嵌套字典 [英] Nested dictionary with Netmiko

查看:89
本文介绍了带有Netmiko的嵌套字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My original question was answered but I am now facing another issue.

获取由字典组成的列表的索引名称

我试图从列表中获取字典的名称,并在for循环中使用它,但似乎无法完成.嵌套词典是解决我的问题的方法.

I was trying to get the name of a dictionary from a list and use it in a for loop but it seems that it can't be done. Nested dictionaries was the solution to my issue.

现在,我可以使用Netmiko成功地将命令发送到网络设备,但似乎它仅使用字典中的最后一个条目.这是代码:

Now I can successfully send command to network devices using Netmiko but it seems it only uses the last entry in the dictionary. Here's the code:

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)中的最后一个条目.这是输出:

The commands are ran successfully but only for the last entry in the nested dictionary (VISW0105). Here's the output:

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:~$

我试图弄清楚为什么它会跳过其他条目.有什么主意吗?

I am trying to figure out why it skips over the other entries. Any idea?

谢谢!

推荐答案

循环如下:

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', {})

此后的所有内容都不在循环中,因为您没有缩进.

Everything after that is not in the loop because you un-indented it.

因此,从循环结束的角度来看,所有内容都引用了site1_switches中的最后一个项目(因为那是我们完成循环的项目).

So from the point that the loop ends, everything refers to the last item in site1_switches (because that's the item on which we finished the loop).

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

Try simply indenting everything below that,

(即

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)

等)

看看会发生什么!

这篇关于带有Netmiko的嵌套字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆