0

my output is

print (network)

['10.202.224.0/24', '10.4.40.0/23', '10.3.41.0/24', '10.4.42.0/21']

can this be somehow converted to

['10.202.224.0/255.255.255.0', '10.4.40.0/255.255.254.0', '10.4.41.0/255.255.255.0', '10.4.42.0/255.255.248.0']

anything that helps converting the cidr to mask.

Thanks in Advance

Ghost rider
  • 43
  • 1
  • 9

1 Answers1

3

you can check out the python code for this may help you by clicking the link below:this is the link to follow

and the code is:

import socket
import struct

def cidr_to_netmask(cidr):
    network, net_bits = cidr.split('/')
    host_bits = 32 - int(net_bits)
    netmask = socket.inet_ntoa(struct.pack('!I', (1 << 32) - (1 << host_bits)))
    return network, netmask

usage:

>>> cidr_to_netmask('10.10.1.32/27')
('10.10.1.32', '255.255.255.224')
>>> cidr_to_netmask('208.128.0.0/11')
('208.128.0.0', '255.224.0.0')
>>> cidr_to_netmask('208.130.28.0/22')
('208.130.28.0', '255.255.252.0')
  • Hi Talha let me know if I am doing it correct as I am not getting any output import socket import struct cidr = ('10.2.5.6/28') def cidr_to_netmask(cidr): network, net_bits = cidr.split('/') host_bits = 32 - int(net_bits) netmask = socket.inet_ntoa(struct.pack('!I', (1 << 32) - (1 << host_bits))) return network, netmask print(cidr) – Ghost rider Dec 06 '21 at 08:33
  • after writing this code you have to pass value of cidr by calling the function name as i mentioned in usage . you can add one line below your existing "cidr_to_netmask(value_you_want_to_pass)" description :first i have called function and passed value. – Talha Yousaf Dec 06 '21 at 08:54