Reputation: 359
How can I convert a ipv4 subnet mask to cidr notation using netaddr
library?
Example: 255.255.255.0 to /24
Upvotes: 24
Views: 57819
Reputation: 6330
As of Python 3.5:
import ipaddress
ip4 = ipaddress.IPv4Network((0,'255.255.255.0'))
print(ip4.prefixlen)
print(ip4.with_prefixlen)
will print:
24
0.0.0.0/24
Upvotes: 4
Reputation: 149736
Using netaddr
:
>>> from netaddr import IPAddress
>>> IPAddress('255.255.255.0').netmask_bits()
24
Using ipaddress
from stdlib:
>>> from ipaddress import IPv4Network
>>> IPv4Network('0.0.0.0/255.255.255.0').prefixlen
24
You can also do it without using any libraries: just count 1-bits in the binary representation of the netmask:
>>> netmask = '255.255.255.0'
>>> sum(bin(int(x)).count('1') for x in netmask.split('.'))
24
Upvotes: 51
Reputation: 98
How about this one? It does not need any additional library as well.
def translate_netmask_cidr(netmask):
"""
Translate IP netmask to CIDR notation.
:param netmask:
:return: CIDR netmask as string
"""
netmask_octets = netmask.split('.')
negative_offset = 0
for octet in reversed(netmask_octets):
binary = format(int(octet), '08b')
for char in reversed(binary):
if char == '1':
break
negative_offset += 1
return '/{0}'.format(32-negative_offset)
It is in some ways similar to IAmSurajBobade's approach but instead does the lookup reversed. It represents the way I would do the conversion manually by pen and paper.
Upvotes: 1
Reputation: 363
Use the following function. it is fast, reliable, and don't use any library.
# code to convert netmask ip to cidr number
def netmask_to_cidr(netmask):
'''
:param netmask: netmask ip addr (eg: 255.255.255.0)
:return: equivalent cidr number to given netmask ip (eg: 24)
'''
return sum([bin(int(x)).count('1') for x in netmask.split('.')])
Upvotes: 7