IP Calculator

This application takes the input of a network address at ‘ipCalc’ (ie:, 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):

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('') if __name__ == ('__main__'): main() [/cc] Output [cc lang="bash" width="100%" height="100%"] This is a private network Network ID: Broadcast IP: Start IP: End IP: Number of assignable IP addresses: 262142 [/cc]

Comments are closed.