##### by Author: Abdelkader Hannache in Domain: it
(edited_by:administ on March 6, 2022, 3:43 p.m.)

Python program solves the problem of calculating addresses of a given network: (from my book: The Conclusion in Computer Systems)

#!/usr/bin/python3

from termcolor import colored

# Calculate the network address (@network ?)

# calculate the first address (@first ?)

# calculate the last address (@last ?)

# Total addresses number (@ntotal ?)

# Division to subnets

# ..

ip = IPNetwork(IP)

print (ip.ip,"=",ip.ip.bits())

print (colored('IP version is:','green'))

print ("IPv",ip.version)

print (colored('Is it Unicast?','green'))

print (ip.ip.is_unicast())

print (colored('Is it Multicast?','green'))

print (ip.ip.is_multicast())

print (colored('Is it Private?','green'))

print (ip.ip.is_private())

print (colored('Is it Public?','green'))

print (ip.ip.is_unicast() and not ip.ip.is_private())

print (colored('@Network is:','green'))

print (ip.network,"=",ip.network.bits())

print (ip[1],"=",ip[1].bits())

print (ip[-2],"=",ip[-2].bits())

print (colored('Number of hosts:(Total addresses Number)','green'))

print (ip.size,"=",bin(ip.size))

print (ip.size-2,"=",bin(ip.size-2))

print (colored('@Subnets:','green'))

subnets=ip.subnet(26)

j=0

for i in subnets:

j = j + 1

print ('subnet',j,'is',i)

print (colored('Reverse IP lookups for DNS:','green'))

print (ip.ip.reverse_dns)

You can name it as "netcalc.py", then run it with:

\$ python3 netcalc.py

Output

192.168.1.18 = 11000000.10101000.00000001.00010010

IP version is:

IPv 4

Is it Unicast?

True

Is it Multicast?

False

Is it Private?

True

Is it Public?

False

255.255.255.0 = 11111111.11111111.11111111.00000000

0.0.0.255 = 00000000.00000000.00000000.11111111

@Network is:

192.168.1.0 = 11000000.10101000.00000001.00000000

192.168.1.255 = 11000000.10101000.00000001.11111111

192.168.1.1 = 11000000.10101000.00000001.00000001

192.168.1.254 = 11000000.10101000.00000001.11111110

256 = 0b100000000

254 = 0b11111110

@Subnets:

subnet 1 is 192.168.1.0/26

subnet 2 is 192.168.1.64/26

subnet 3 is 192.168.1.128/26

subnet 4 is 192.168.1.192/26

Reverse IP lookups for DNS: