Home > Blockchain >  Obtain decimal netmask from prefix length python 3.x
Obtain decimal netmask from prefix length python 3.x

Time:10-22

I created this code because I was not able to find any functional that accomplishes my requirement.

If you can reduce it will be better.

Just enter de prefix lenght from 1 to 32 and you will get the decimal mask. This code help me with my scripts for cisco.

import math

#Netmask octets
octet1 = [0,0,0,0,0,0,0,0]
octet2 = [0,0,0,0,0,0,0,0]
octet3 = [0,0,0,0,0,0,0,0]
octet4 = [0,0,0,0,0,0,0,0]

#POW list
pow_list = [7,6,5,4,3,2,1,0]

#Introduce prefix lenght
mask = int(input("Introduce the prefix lenght: "))

#According to the number of bits we will change the array elements from 0 to 1
while mask >= 25 and mask <= 32:
    octet4[mask-25] = 1
    mask -= 1

while mask >= 17 and mask <= 24:
    octet3[mask-17] = 1
    mask -= 1

while mask >= 9 and mask <= 16:
    octet2[mask-9] = 1
    mask -= 1

while mask >= 1 and mask <= 8:
    octet1[mask-1] = 1
    mask -= 1

#Obtain the number of ones
ones1 = octet1.count(1)
ones2 = octet2.count(1)
ones3 = octet3.count(1)
ones4 = octet4.count(1)

#Summary and reuslt of each octet.
sum1 = 0
for i in range(0,ones1):
    sum1 = sum1   math.pow(2,pow_list[i])
sum1 = int(sum1)

sum2 = 0
for i in range(0,ones2):
    sum2 = sum2   math.pow(2,pow_list[i])
sum2 = int(sum2)

sum3 = 0
for i in range(0,ones3):
    sum3 = sum3   math.pow(2,pow_list[i])
sum3 = int(sum3)

sum4 = 0
for i in range(0,ones4):
    sum4 = sum4   math.pow(2,pow_list[i])
sum4 = int(sum4)

#Join the results with a "."
decimal_netmask = str(sum1)   "."   str(sum2)   "."   str(sum3)   "."   str(sum4)

#Result
print("Decimal netmask is: "  decimal_netmask)

Result: Introduce the prefix lenght: 23 Decimal netmask is: 255.255.254.0

CodePudding user response:

You can simplify your code by computing the overall mask value as an integer using the formula:

mask = 2**32 - 2**(32-prefix_length)

Then you can compute the 4 8-bit parts of the mask (by shifting and masking), appending the results to a list and then finally joining each element of the list with .:

def decimal_netmask(prefix_length):
    mask = 2**32 - 2**(32-prefix_length)
    octets = []
    for _ in range(4):
        octets.append(str(mask & 255))
        mask >>= 8
    return '.'.join(reversed(octets))

for pl in range(33):
    print(f'{pl:3d}\t{decimal_netmask(pl)}')

Output:

  0     0.0.0.0
  1     128.0.0.0
  2     192.0.0.0
  3     224.0.0.0
  4     240.0.0.0
  5     248.0.0.0
  6     252.0.0.0
  7     254.0.0.0
  8     255.0.0.0
  9     255.128.0.0
 10     255.192.0.0
 11     255.224.0.0
 12     255.240.0.0
 13     255.248.0.0
 14     255.252.0.0
 15     255.254.0.0
 16     255.255.0.0
 17     255.255.128.0
 18     255.255.192.0
 19     255.255.224.0
 20     255.255.240.0
 21     255.255.248.0
 22     255.255.252.0
 23     255.255.254.0
 24     255.255.255.0
 25     255.255.255.128
 26     255.255.255.192
 27     255.255.255.224
 28     255.255.255.240
 29     255.255.255.248
 30     255.255.255.252
 31     255.255.255.254
 32     255.255.255.255

CodePudding user response:

As you are probably doing more than just converting CIDR to netmask, I recommend checking out the built-in library ipaddress

from ipaddress import ip_network
cidr = input("Introduce the prefix length: ")
decimal_netmask = str(ip_network(f'0.0.0.0/{cidr}').netmask)
  • Related