Let's say my local ip is 10.212.97.23
and my Subnet Mask is 255.255.254.0
According to that, I'm in the same subnet with 10.212.96.*
and CIDR would be 10.212.96.0/23
So my question is: Is there any python library which takes local ip and subnet mask and calculates the CIDR value? I checked netaddr library but couldn't find this functionality
The basic count in CIDR can be computed manually using the below function without using any libraries:
>>> netmask = "255.255.254.0"
>>> sum([bin(int(x)).count("1") for x in netmask.split(".")])
23
Let me know if it is helpful.
If you want to do it with a module, the ipaddress module can help:
❯ python3
>>> import ipaddress
>>> ipaddress.IPv4Network("10.212.97.23/255.255.254.0", strict=False).prefixlen
23
Docs here .
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.