我正在尝试从键'ip'中提取特定值,在此示例中为192.168.200.200,但是在某些情况下,它会有所不同,并且可能不止一个。我是python新手,有人可以帮我提取这些值吗?
# import functions
from cisco_xe_api import *
# define variables
device_config = api_get_conf()
# Rule SV-105995r2_rule: The Cisco router must be configured to implement message
# authentication for all control plane protocols.
def sv105995r2rule_ospf():
#device_config = api_get_conf()
routing_protocol = device_config['Cisco-IOS-XE-native:native']['router']
ospf_networks = device_config['Cisco-IOS-XE-native:native']['router']['Cisco-IOS-XE-ospf:router-ospf']['ospf']
protocol_intf = device_config['Cisco-IOS-XE-native:native']['interface']
if 'Cisco-IOS-XE-ospf:router-ospf' in routing_protocol.keys():
print('\nOSPF is configured on this device. Checking for MD5 authentication.'
print(ospf_networks.items())
这里是打印语句的输出:
OSPF is configured on this device. Checking for MD5 authentication.
dict_items([('process-id', [{'id': 100, 'area': [{'area-id': 0, 'authentication': {'message-digest': [None]}}], 'network': [{'ip': '192.168.200.200', 'wildcard': '0.0.0.0', 'area': 0}]}])])
感谢您的帮助!
结构是字典→列表→字典→列表→字典,因此在您的示例中,您可以像这样获得IP:
ospf_networks['process-id'][0]['network'][0]['ip']
我对您所使用的API一无所知,但假设任何列表中可能包含多个项目,则打印所有IP看起来像这样:
for d0 in ospf_networks['process-id']:
for d1 in d0['network']:
print(d1['ip'])
对于您提供的示例,获取ip的代码为:
ospf_networks['process_id'][0]['network'][0]['ip']
它是这样的:
现在要获得多个IP地址ospf_networks指示,它将完全取决于它们在结构中的位置。