import ipaddress as ip
CLASS_C_ADDR ='192.0.2.0'
if __name__ == '__main__':
not_exit =True
while not_exit:
prefix = input('Enter the prefix length(24-30):')
prefix =int(prefix)
if prefix not in range(24,31):
raise Exception('Prefix length must be between 24 and 30')
net_addr = CLASS_C_ADDR+'/'+str(prefix)
print('Using network address:',net_addr)
net4= ip.ip_network(net_addr)
print('This prefix will give',net4.num_addresses,'IP address')
print('The network configuration will be')
print('\t Network address:',str(net4.network_address))
print('\t Subnet mask:',str(net4.netmask))
print('\t Broadcast address:',str(net4.broadcast_address))
first_ip,last_ip = list(net4.hosts())[0],list(net4.hosts())[-1]
print('\t Host IP address:from',first_ip,'to',last_ip)
if prefix < 30:
all_subnets = list(net4.subnets())
print('\t Largest Subnet information:')
for s in all_subnets:
print('\t\t',s)
ok =input('Enter y to exit,or any key to continue [y/n]')
ok =ok.lower()
if ok.strip()=='y':
not_exit = False