基于键值组合列表

时间:2021-07-14 20:16:55

标签: python list cisco

我有一个包含多个 show 命令的脚本,我想根据接口值将它们合并到一个列表中

from MY_ATH import ME
from netmiko import ConnectHandler
import json

#Enter in the IP of the switch, username and password
IP = input('IP of switch: ')
username = ME['username']
password = ME['password']

SW = {
    'ip':   IP,
    'username': username,
    'password': password,
    'device_type': 'cisco_ios',
}

net_connect = ConnectHandler(**SW)

#This script has hardcoded voice vlan info

#does this command and looks at the structured data
interfaces = net_connect.send_command('show interfaces switchport', use_textfsm=True)
#cdp = net_connect.send_command('show interfaces switchport', use_textfsm=True)
#cdp2 = net_connect.send_command('show interfaces description', use_textfsm=True)
#print(json.dumps(interfaces, indent=2))
#print(json.dumps(cdp3, indent=2))

interface_list = []
for interface in interfaces:
    result={}
    result["interface"] = interface['interface']
    result["admin_mode"] = interface['admin_mode']
    result["access_vlan"] = interface['access_vlan']
    result["voice_vlan"] = interface['voice_vlan']
    result["trunking_vlans"] = interface['trunking_vlans']

    interface_list.append(result)

print (json.dumps(interface_list, indent=2))

interfaces1 = net_connect.send_command('show interfaces description', use_textfsm=True)
#print(json.dumps(interfaces1, indent=2))

interface_list1 = []
for interface in interfaces1:
    result={}
    result["interface"]=interface['port']
    result["descrip"] = interface['descrip']

    interface_list1.append(result)

print (json.dumps(interface_list1, indent=2))

interfaces2 = net_connect.send_command('show lldp neighbors detail', use_textfsm=True)
#print(json.dumps(interfaces2, indent=2))
interface_list2 = []
for interface in interfaces2:
    result={}
    result["interface"]=interface['local_interface']
    result["neighbor"] = interface['neighbor']
    interface_list2.append(result)

print (json.dumps(interface_list2, indent=2))
print(interface_list)
print(interface_list1)
print(interface_list2)

for key in interface_list:
    print (key)
for key in interface_list1:
    print (key)
for key in interface_list2:
    print (key)

我得到的打印信息是这样的:

{'interface': 'Gi0/1', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': 'none', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/2', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': '2122', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/3', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': 'none', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/4', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': 'none', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/5', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': 'none', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/6', 'admin_mode': 'static access', 'access_vlan': '1035', 'voice_vlan': 'none', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/7', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': '2122', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/8', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': '2122', 'trunking_vlans': ['ALL']}
{'interface': 'Gi0/9', 'admin_mode': 'trunk', 'access_vlan': '1', 'voice_vlan': 'none', 'trunking_vlans': ['340,503,1001,1035,1500,2122,2430,3216,3350']}
{'interface': 'Gi0/10', 'admin_mode': 'trunk', 'access_vlan': '1', 'voice_vlan': 'none', 'trunking_vlans': ['503,1001,1115,1175,1177,2020,2122,2430,3216,3227']}
{'interface': 'Vl1', 'descrip': ''}
{'interface': 'Vl1001', 'descrip': ''}
{'interface': 'Gi0/1', 'descrip': 'Interface Gi0/1'}
{'interface': 'Gi0/2', 'descrip': 'Interface Gi0/2'}
{'interface': 'Gi0/3', 'descrip': 'Interface Gi0/3'}
{'interface': 'Gi0/4', 'descrip': 'Interface Gi0/4'}
{'interface': 'Gi0/5', 'descrip': 'Interface Gi0/5'}
{'interface': 'Gi0/6', 'descrip': 'Interface Gi0/6'}
{'interface': 'Gi0/7', 'descrip': 'Interface Gi0/7'}
{'interface': 'Gi0/8', 'descrip': 'Interface Gi0/8'}
{'interface': 'Gi0/9', 'descrip': 'to 501_2nd_10.0.1.20.ne.gov'}
{'interface': 'Gi0/10', 'descrip': 'to 0.0.1.20'}
{'interface': 'Gi0/9', 'neighbor': '501_2nd_10.0.1.20.ne.gov'}
{'interface': 'Gi0/2', 'neighbor': 'SEP08CCA7845AC3.nebraska.gov'}
{'interface': 'Gi0/1', 'neighbor': ''}
{'interface': 'Gi0/7', 'neighbor': ''}
{'interface': 'Gi0/5', 'neighbor': ''}

我想将所有这些合并成这样的:

{'interface': 'Gi0/1',
 'admin_mode': 'static access',
 'access_vlan': '3216',
 'voice_vlan': 'none',
 'trunking_vlans': ['ALL'],
 'descrip': 'Interface Gi0/1',
 'neighbor': ''}

1 个答案:

答案 0 :(得分:0)

实现此目的的一种方法是使用一个以接口为键的字典和一个内部字典作为您使用该接口的每个新结果更新的值。

outputs = [
    # Only the 'Gi0/1' and 'Gi0/2' interfaces are used in this example.
    {'interface': 'Gi0/1', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': 'none', 'trunking_vlans': ['ALL']},
    {'interface': 'Gi0/2', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': '2122', 'trunking_vlans': ['ALL']},
    {'interface': 'Gi0/1', 'descrip': 'Interface Gi0/1'},
    {'interface': 'Gi0/2', 'descrip': 'Interface Gi0/2'},
    {'interface': 'Gi0/2', 'neighbor': 'SEP08CCA7845AC3.nebraska.gov'},
    {'interface': 'Gi0/1', 'neighbor': ''},
]

results = {}

for output in outputs:
    interface = output['interface']
    try:
        interface_dict = results[interface]
    except KeyError:
        # This interface not in results yet. Add it.
        interface_dict = {}
        results[interface] = interface_dict
    # Update interface_dict with new values from output
    interface_dict.update(output)

print(results)

其中 results 看起来像这样:

{
    'Gi0/1': {'interface': 'Gi0/1', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': 'none', 'trunking_vlans': ['ALL'], 'descrip': 'Interface Gi0/1', 'neighbor': ''},
    'Gi0/2': {'interface': 'Gi0/2', 'admin_mode': 'static access', 'access_vlan': '3216', 'voice_vlan': '2122', 'trunking_vlans': ['ALL'], 'descrip': 'Interface Gi0/2', 'neighbor': 'SEP08CCA7845AC3.nebraska.gov'}
}

在您的脚本中,您可以将所有输出保存在一个列表中,并在最后进行组合,如上所示。

另一种方法是创建一个函数,该函数接受结果字典并将其合并到其中,并在每次获得更多数据时执行此操作。如果您有大量数据并且不想坚持到最后,这将非常有用。