用python检查{ "network_scenarios": "sdn", "vlan_planes": [ ], "vxlan_planes": [ { "network_type": "DATAPLANE", "name": "default", "vlan_id": None, "vni_start": 10000, "vni_end": 20000, "svlan_start": "", "svlan_end": "", "vdp_vlan_id": "", "mirror_svlan_start": "", "mirror_svlan_end": "", "mirror_vdp_pkt_vlan": "", "segmentation_type": "vxlan", "ip_ranges": [ { "cidr": "166.166.16.0/24", "gateway": "166.166.16.1", "start": "166.166.16.10", "end": "166.166.16.166", "vlan_id": "" } ] } ], "geneve_planes": [] }中的cidr是否配置?cidr格式是否正确?cidr是否在start和end之间?
时间: 2023-07-13 18:30:46 浏览: 106
可以使用以下Python代码来检查cidr是否配置、格式是否正确以及是否在start和end之间:
```
import ipaddress
data = {
"network_scenarios": "sdn",
"vlan_planes": [],
"vxlan_planes": [
{
"network_type": "DATAPLANE",
"name": "default",
"vlan_id": None,
"vni_start": 10000,
"vni_end": 20000,
"svlan_start": "",
"svlan_end": "",
"vdp_vlan_id": "",
"mirror_svlan_start": "",
"mirror_svlan_end": "",
"mirror_vdp_pkt_vlan": "",
"segmentation_type": "vxlan",
"ip_ranges": [
{
"cidr": "166.166.16.0/24",
"gateway": "166.166.16.1",
"start": "166.166.16.10",
"end": "166.166.16.166",
"vlan_id": ""
}
]
}
],
"geneve_planes": []
}
cidr = data["vxlan_planes"][0]["ip_ranges"][0]["cidr"]
start = data["vxlan_planes"][0]["ip_ranges"][0]["start"]
end = data["vxlan_planes"][0]["ip_ranges"][0]["end"]
# Check if cidr is configured
if cidr:
# Check if cidr format is correct
try:
ipaddress.IPv4Network(cidr)
except ValueError:
print("Error: cidr format is incorrect")
else:
# Check if cidr is within start and end range
if ipaddress.IPv4Address(start) <= ipaddress.IPv4Address(cidr.split('/')[0]) <= ipaddress.IPv4Address(end):
print("cidr is configured and within start and end range")
else:
print("Error: cidr is not within start and end range")
else:
print("Error: cidr is not configured")
```
这段代码会输出 "cidr is configured and within start and end range",表示cidr已经配置且在start和end之间。
阅读全文