IP Calculator

This application takes the input of a network address at ‘ipCalc’ (ie: 10.255.200.200/14), and outputs the classification of the network, along with it’s start and stop IP, broadcast IP, and number of assignable IP addresses within the range.
[cc lang=”python” width=”100%” height=”100%”]
import ipaddress
import sys
def ipCalc(targetIP):
try:
ip = ipaddress.IPv4Network(targetIP, strict=False)
firstOctet = int(str(ip).split(‘.’)[0])
ipClass = “”
if firstOctet == 0:
return print(“This is a source IP”)
elif firstOctet >= 1 and firstOctet <= 126:
ipClass = "Class A Network"
elif firstOctet == 127:
return print("This is a loopback IP address")
elif firstOctet >=128 and firstOctet <= 191:
ipClass = "Class B Network"
elif firstOctet >= 192 and firstOctet <= 223:
ipClass = "Class C Network"
elif firstOctet >= 224 and firstOctet <= 239:
ipClass = "Class D Network"
else:
return print("Class E Network")
except ipaddress.AddressValueError:
return print("Error: Invalid IP Address")
except ipaddress.SubnetValueError:
return print("Error: Invalid Subnet Mask")
if ip.is_private:
print('This is a private network')
print("Network ID: {}".format(ip.with_netmask))
print("Broadcast IP: {}".format(ip.broadcast_address))
print("Start IP: {}".format(ip.network_address +1))
print("End IP: {}".format(ip.broadcast_address -1))
print("Number of assignable IP addresses: {}".format(ip.num_addresses -2))
def main():
ipCalc('10.254.200.200/14')
if __name__ == ('__main__'):
main()
[/cc]
Output
[cc lang="bash" width="100%" height="100%"]
This is a private network
Network ID: 10.252.0.0/255.252.0.0
Broadcast IP: 10.255.255.255
Start IP: 10.252.0.1
End IP: 10.255.255.254
Number of assignable IP addresses: 262142
[/cc]
Comments are closed.