How do I convert a IPv4 subnet mask to CIDR range? I have subnet and gateway information available.
Data available:
I know using ipaddress
, CIDR value can be obtained.
from ipaddress import IPv4Network
ip4 = IPv4Network('0/255.255.255.0')
print(ip4.prefixlen)
print(ip4.with_prefixlen)
with results
24
0.0.0.0/24
Pass both your gateway and mask as a tuple to IPv4Network
import ipaddress
gateway = '192.0.2.250'
mask = '255.255.255.0'
network = ipaddress.IPv4Network((gateway, mask), strict=False)
print(network) # 192.0.2.0/24
You must pass strict=False
because the network has host bits set
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.