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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()

Output

1
2
3
4
5
6
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