If you work with Cisco devices, especially when migrating from Nexus OS to IOS XE, you know that Nexus uses CIDR notation while IOS XE prefers subnet and wildcard masks. In this post, I’ll show a quick way to convert CIDR to subnet and wildcard masks using Python, making it easier to update static routes and access lists.
IPv4/6 Manipulation in Python
While doing a conversion from Nexus OS to IOS XE, I came across the Python library ipaddress
.
It can easily convert CIDR notation to subnet and wildcard masks, which are essential when converting static routes or building access lists in IOS XE — since Nexus uses CIDR notation, whereas IOS XE uses subnet/wildcard masks.
Here are two quick functions to convert CIDR notation to a subnet mask and a wildcard mask for use in Static Routes and Access Lists on Cisco IOS XE.
import ipaddress
def cidr_to_subnet_mask(cidr):
ip = ipaddress.IPv4Network(cidr, strict=False)
return f"{ip.network_address} {ip.netmask}"
def cidr_to_wildcard(cidr):
ip = ipaddress.IPv4Network(cidr, strict=False)
return f"{ip.network_address} {ip.hostmask}"
if __name__ == "__main__":
cidr = "10.10.10.1/29"
ip_and_mask = cidr_to_subnet_mask(cidr)
print(ip_and_mask)
ip_and_wildcard = cidr_to_wildcard(cidr)
print(ip_and_wildcard)
Outputs
10.10.10.0 255.255.255.248
10.10.10.0 0.0.0.7