diff --git a/README.md b/README.md index 9688f7c7..9e821373 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ More information on contributing and the general code of conduct for discussion | Snake Water Gun | [Snake Water Gun](https://github.com/DhanushNehru/Python-Scripts/tree/master/Snake-Water-Gun) | A game similar to Rock Paper Scissors. | | Sorting | [Sorting](https://github.com/DhanushNehru/Python-Scripts/tree/master/Sorting) | Algorithm for bubble sorting. | | Star Pattern | [Star Pattern](https://github.com/DhanushNehru/Python-Scripts/tree/master/Star%20Pattern) | Creates a star pattern pyramid. | +| Subnetting Calculator | [Subnetting Calculator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Subnetting%20Calculator) | Gives a useful information based on an IP address and subnet mask, such as the range of usable host IPs, network, and broadcast address. | | Take a break | [Take a break](https://github.com/DhanushNehru/Python-Scripts/tree/master/Take%20A%20Break) | Python code to take a break while working long hours. | | Text to Image | [Text to Image](https://github.com/DhanushNehru/Python-Scripts/tree/master/Text-to-Image) | A Python script that will take your text and convert it to a JPEG. | | Text Recognition | [Text Recognition](https://github.com/DhanushNehru/Python-Scripts/tree/Text-Recognition/Text%20Recognition) | A Image Text Recognition ML Model to extract text from Images | diff --git a/Subnetting Calculator/README.md b/Subnetting Calculator/README.md new file mode 100644 index 00000000..6c0f5a76 --- /dev/null +++ b/Subnetting Calculator/README.md @@ -0,0 +1,15 @@ +# How to Run +- Make sure you have python 3.8.10 installed +- Download SubnetCalc.py +- Run the program with +```markdown +./whereverfileislocated> python SubnetCalc.py +``` + +# How to use +- When ran, you are prompted for a IP address and subnet mask +- After entering in IP and subnet mask, you are given results from a subnet Calculator +- You may choose to exit the program by entering "y" when prompted to + +## Dependencies: +- math library diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py new file mode 100644 index 00000000..e77911e7 --- /dev/null +++ b/Subnetting Calculator/SubnetCalc.py @@ -0,0 +1,75 @@ +import math + + +def formatToAddress(ip: list): + ipString = "" + for num in range(4): + ipString += str(ip[num])+"." + ipString = ipString[:-1] + return ipString + + +while True: + while True: + ipAddress = input("Give a valid IPv4 address: ") + ipAddress = ipAddress.replace(" ", "") + subnetMask = input("Give a valid Subnet Mask: ") + subnetMask = subnetMask.replace(" ", "") + + binaryIP = ipAddress.split(".") + binarySubnet = subnetMask.split(".") + if len(binarySubnet) != 4 or len(binaryIP) != 4: + print("\nPlease put in a valid IP/Subnet Mask") + + try: + for i in range(4): + binaryIP[i] = '{0:08b}'.format(int(binaryIP[i])) + binarySubnet[i] = '{0:08b}'.format(int(binarySubnet[i])) + break + except ValueError: + print("Please valid numbers in between the \".\"") + + binaryNetworkAddress = ["", "", "", ""] + binaryBroadcastAddress = ["", "", "", ""] + for i in range(4): + subnetBits = binarySubnet[i] + ipBits = binaryIP[i] + for j in range(8): + subnetBit = subnetBits[j:j+1] + ipBit = ipBits[j:j+1] + binaryNetworkAddress[i] += str(int(ipBit)*int(subnetBit)) + if (subnetBit == "1"): + binaryBroadcastAddress[i] += ipBit + else: + binaryBroadcastAddress[i] += "1" + + networkAddress = [0]*4 + broadcastAddress = [0]*4 + totalNumOfHosts = 0 + for i in range(4): + networkAddress[i] = int(binaryNetworkAddress[i], 2) + broadcastAddress[i] = int(binaryBroadcastAddress[i], 2) + # Counts the number of zeroes in a subnet mask + totalNumOfHosts += binarySubnet[i].count("0") + # 2^# of zeroes is how to find the total number of hosts + totalNumOfHosts = math.pow(2, totalNumOfHosts) + + firstUsableAddress = networkAddress.copy() + firstUsableAddress[3] += 1 + lastUsableAddress = broadcastAddress.copy() + lastUsableAddress[3] -= 1 + + print("_____________________________________________\n") + print("IPv4 Address: | "+ipAddress) + print("Subnet Mask: | "+subnetMask) + print("Network Address: | "+formatToAddress(networkAddress)) + print("Broadcast Address: | "+formatToAddress(broadcastAddress)) + print("Range of Usable Host IPs: | "+formatToAddress(firstUsableAddress) + + " - "+formatToAddress(lastUsableAddress)) + print("Total Number of Hosts: | "+str(totalNumOfHosts)) + print("Total Number of Usable Hosts: | "+str(totalNumOfHosts-2)) + print("Binary Subnet Mask: | " + str(binaryIP)) + print("Binary IP address: | "+str(binarySubnet)) + + if (input("\nDo you want to exit this program? (y/n) ") == "y"): + break