【问题标题】:How to get rid of dictionary number when dynamically building on动态构建时如何摆脱字典编号
【发布时间】:2021-06-21 19:16:29
【问题描述】:
device_total={}
user_n="Cisco"
pwd= "test"
count=1
for i in range(0,31):
    ip_addr= input('Please enter the device IP address' + ' : ')
    dev_type = input (" Enter device type" + ' : ')
    dsum = {'device type':dev_type,'host':ip_addr,'username':user_n,'password':pwd}
    device_total[i] = dsum
    i+1
    query_user=input('Do you want to add additional devices? (y/n)' )
    if query_user != 'y':
       break
print(device_total)

我正在尝试根据用户输入动态创建多个字典,以用于使用 Netmiko 配置网络设备,这些字典已构建但每个字典都有编号,这会在 Netmiko 功能尝试使用时导致行错误他们登录后进行配置更改。请让我知道是否有办法在没有它们的情况下创建字典。我想为用户输入的每个设备创建一个单独的字典,而不是一个包含多个条目的字典。谢谢。

请看下面的例子:

{0: {'device type': 'cisco', 'host': '1.1.1.1', 'username': 'Cisco', 'password': 'test'}, 1: {'device type': 'Arista', 'host': '2.2.2.2', 'username': 'Cisco', 'password': 'test'}}

【问题讨论】:

  • 我怀疑您根本不需要字典,而是需要一个列表。我建议device_total=[],然后是device_total.append( dsum ),而不是device_total[i] = dsum
  • 谢谢,但我确实需要一本字典。标题错误,因为我的问题是错误的,一旦嵌套字典,我就会尝试拆分它们。我一直在网上寻找解决方案,貌似没有人遇到过这个问题。
  • 由于您尚未向我们展示您将如何使用此信息,我们无能为力。请注意,device_total.values() 将返回所有没有键的值。

标签: python dictionary


【解决方案1】:
device_total=[]
user_n="Cisco"
pwd= "test"
count=1
for i in range(0,31):
    ip_addr= input('Please enter the device IP address' + ' : ')
    dev_type = input (" Enter device type" + ' : ')
    dsum = {'device type':dev_type,'host':ip_addr,'username':user_n,'password':pwd}
    device_total.append( dsum)
    i+1
    query_user=input('Do you want to add additional devices? (y/n)' )
    if query_user != 'y':
       break
print(device_total)

【讨论】:

    【解决方案2】:

    这是我为可能处于相同情况并卡住的其他人提出的解决方案。

    from getpass import getpass
    from netmiko import ConnectHandler
    from netmiko import NetmikoAuthenticationException
    
    
    def user_input():
        """ Getting user credentials """
        usr_n = input("username: ")
        pwd = getpass()
        cred = (usr_n, pwd)
    
        return cred
    
    
    def net_devices():
        login_info = user_input()
        username = login_info[0]
        password = login_info[1]
    
        with open("districts devices.txt") as f:
    
            net_devices = f.read().splitlines()
    
        for device in net_devices:
            try:
                netw_devices = {"device_type": 'cisco_ios', 'host': device, 'username': username, 'password': password}
                net_connect = ConnectHandler(**netw_devices)
            except NetmikoAuthenticationException:
                print(f"Unable to login to {device}. Verify you have the right credentials or it is added to ISE.")
                continue
    
            """ Verifying the access-list has been applied to the device. """
    
            print('Verifying ACL the is on: ' + device)
    
            verification = net_connect.send_command('show run | b line vty')
    
            if "access-class" not in verification:
                print("\nThe access-class is missing from this device's configuration. Configuring the ACL on: "
                      + device)
                acl_cmds = ['service password-encryption', 'ip access-list standard Sample-ONLY',
                            'permit 10.0.0.0 0.255.255.255',
                            ' permit 172.16.0.0 0.0.255.255', 'line vty 0 15', 'transport in ssh',
                            'access-class sample in', 'do write memory']
                commands = net_connect.send_config_set(acl_cmds)
                print(commands)
                
    
    
            else:
                print("\nNo update necessary.\n")
    
                # print(" \nVerifying the ACL is now in place.")
                # re_verification = net_connect.send_command('\nshow run | b line vty')
                # print(re_verification)
    
                net_connect.disconnect()
    
    
    net_devices()
    
    

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 2018-07-19
      • 2017-01-24
      • 2021-04-30
      • 1970-01-01
      相关资源
      最近更新 更多