face

Network addresses calculation

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

#requirements: pip3 install termcolor ipaddress

import ipaddress

from netaddr import *

from termcolor import colored

 

# Computing the Network Addresses:

# Calculate the network address (@network ?)

# Calculate the broadcast address (@broadcast ?)

# calculate the first address (@first ?)

# calculate the last address (@last ?)

# Total addresses number (@ntotal ?)

# Addressable addresses (@addr)

# Division to subnets

# ..

 

IP = input(colored('Give IP Address with netmask like 192.168.1.2/24 to Calculate Network Addresses: ','blue'))

ip = IPNetwork(IP)

 

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

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('@Netmask is:','green'))

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

 

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

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

 

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

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

 

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

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

 

print (colored('First address is:','green'))

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

 

print (colored('Last address is:','green'))

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

 

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

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

 

print (colored('Addressable addresses:','green'))

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

Give IP Address with netmask like 192.168.1.2/24 to Calculate Network Addresses: 192.168.1.18/24

Your IP is:

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

@Netmask is:

255.255.255.0 = 11111111.11111111.11111111.00000000

@Hostmask is:

0.0.0.255 = 00000000.00000000.00000000.11111111

@Network is:

192.168.1.0 = 11000000.10101000.00000001.00000000

@Broadcast is:

192.168.1.255 = 11000000.10101000.00000001.11111111

First address is:

192.168.1.1 = 11000000.10101000.00000001.00000001

Last address is:

192.168.1.254 = 11000000.10101000.00000001.11111110

Number of hosts:(Total addresses Number)

256 = 0b100000000

Addressable addresses:

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:

18.1.168.192.in-addr.arpa.

 

Available in Github platform:

https://github.com/kaderdevlino/netcalc