From 24cb8c5e6efea6f23162c95763c52f8965527a0b Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:27:36 +0000 Subject: [PATCH 01/10] feat(SubnetCalc): can turn IPv4 address to binary --- Subnetting Calculator/SubnetCalc.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Subnetting Calculator/SubnetCalc.py diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py new file mode 100644 index 00000000..0efd3cf8 --- /dev/null +++ b/Subnetting Calculator/SubnetCalc.py @@ -0,0 +1,9 @@ +ipAddress = input("Give a valid IPv4 address: ") +ipAddress = ipAddress.replace(" ", "") + +tempIp = ipAddress.split(".") +binaryIP = [0, 0, 0, 0] +for i in range(4): + binaryIP[i] = '{0:08b}'.format(int(tempIp[i])) + + From 39f4400662adbde50f6aee4d1df8ef540e19c0ba Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:32:03 +0000 Subject: [PATCH 02/10] feat(SubnetCalc): can turn subnet mask to binary --- Subnetting Calculator/SubnetCalc.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py index 0efd3cf8..14af33b0 100644 --- a/Subnetting Calculator/SubnetCalc.py +++ b/Subnetting Calculator/SubnetCalc.py @@ -1,9 +1,17 @@ ipAddress = input("Give a valid IPv4 address: ") ipAddress = ipAddress.replace(" ", "") +subnetMask = input("Give a valid Subnet Mask: ") +subnetMask = subnetMask.replace(" ", "") tempIp = ipAddress.split(".") +tempSubnetMask = subnetMask.split(".") + binaryIP = [0, 0, 0, 0] +binarySubnet = [0, 0, 0, 0] for i in range(4): binaryIP[i] = '{0:08b}'.format(int(tempIp[i])) + binarySubnet[i] = '{0:08b}'.format(int(tempSubnetMask[i])) +print(binaryIP) +print(binarySubnet) From 6e8ae4c2a5caca9d350c860cb1e38c943cc2060b Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Fri, 13 Oct 2023 20:43:20 +0000 Subject: [PATCH 03/10] feat(SubnetCalc): can find binary of Network # --- Subnetting Calculator/SubnetCalc.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py index 14af33b0..2a913710 100644 --- a/Subnetting Calculator/SubnetCalc.py +++ b/Subnetting Calculator/SubnetCalc.py @@ -12,6 +12,14 @@ binaryIP[i] = '{0:08b}'.format(int(tempIp[i])) binarySubnet[i] = '{0:08b}'.format(int(tempSubnetMask[i])) +binaryNetworkNum=["","","",""] +for i in range(4): + subnetBits = binarySubnet[i] + ipBits = binaryIP[i] + for j in range(8): + binaryNetworkNum[i]+=str(int(ipBits[j:j+1])*int(subnetBits[j:j+1])) + print(binaryIP) print(binarySubnet) +print(binaryNetworkNum) From 7e9685b608c1006e3c6e81f92b292766dc07b086 Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Sat, 14 Oct 2023 17:56:18 +0000 Subject: [PATCH 04/10] feat(SubnetCalc): finished --- Subnetting Calculator/SubnetCalc.py | 61 +++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py index 2a913710..c1f4d29c 100644 --- a/Subnetting Calculator/SubnetCalc.py +++ b/Subnetting Calculator/SubnetCalc.py @@ -1,25 +1,62 @@ +import math + +def formatToAddress(ip : list): + ipString = "" + for i in range(4): + ipString += str(ip[i])+"." + ipString = ipString[:-1] + + return ipString + ipAddress = input("Give a valid IPv4 address: ") ipAddress = ipAddress.replace(" ", "") subnetMask = input("Give a valid Subnet Mask: ") subnetMask = subnetMask.replace(" ", "") -tempIp = ipAddress.split(".") -tempSubnetMask = subnetMask.split(".") - -binaryIP = [0, 0, 0, 0] -binarySubnet = [0, 0, 0, 0] +binaryIP = ipAddress.split(".") +binarySubnet = subnetMask.split(".") for i in range(4): - binaryIP[i] = '{0:08b}'.format(int(tempIp[i])) - binarySubnet[i] = '{0:08b}'.format(int(tempSubnetMask[i])) + binaryIP[i] = '{0:08b}'.format(int(binaryIP[i])) + binarySubnet[i] = '{0:08b}'.format(int(binarySubnet[i])) -binaryNetworkNum=["","","",""] +binaryNetworkAddress = ["", "", "", ""] +binaryBroadcastAddress = ["", "", "", ""] for i in range(4): subnetBits = binarySubnet[i] ipBits = binaryIP[i] for j in range(8): - binaryNetworkNum[i]+=str(int(ipBits[j:j+1])*int(subnetBits[j:j+1])) + 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) -print(binaryIP) -print(binarySubnet) -print(binaryNetworkNum) +firstUsableAddress = networkAddress.copy() +firstUsableAddress[3] += 1 +lastUsableAddress = broadcastAddress.copy() +lastUsableAddress[3] -= 1 +print("_____________________________________________\n") +print("IPv4 Address: "+ipAddress) +print("Subnet Mask: "+subnetMask) +print("Range of IPs: "+formatToAddress(networkAddress) + + " - "+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)) From d1318d5477c031a4f3cc875cbfd74c4edf6a62a0 Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:03:47 +0000 Subject: [PATCH 05/10] feat(SubnetCalc): prevents invalid inputs --- Subnetting Calculator/SubnetCalc.py | 36 ++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py index c1f4d29c..693749f7 100644 --- a/Subnetting Calculator/SubnetCalc.py +++ b/Subnetting Calculator/SubnetCalc.py @@ -1,6 +1,7 @@ import math -def formatToAddress(ip : list): + +def formatToAddress(ip: list): ipString = "" for i in range(4): ipString += str(ip[i])+"." @@ -8,16 +9,25 @@ def formatToAddress(ip : list): return ipString -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(".") -for i in range(4): - binaryIP[i] = '{0:08b}'.format(int(binaryIP[i])) - binarySubnet[i] = '{0:08b}'.format(int(binarySubnet[i])) +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 = ["", "", "", ""] @@ -35,12 +45,12 @@ def formatToAddress(ip : list): networkAddress = [0]*4 broadcastAddress = [0]*4 -totalNumOfHosts= 0 +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") + totalNumOfHosts += binarySubnet[i].count("0") # 2^# of zeroes is how to find the total number of hosts totalNumOfHosts = math.pow(2, totalNumOfHosts) @@ -58,5 +68,5 @@ def formatToAddress(ip : list): " - "+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 Subnet Mask: " + str(binaryIP)) print("Binary IP address: "+str(binarySubnet)) From f151b4c87e404d95fcdbbab7e82466c745d0c801 Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:09:34 +0000 Subject: [PATCH 06/10] feat(SubnetCalc): User may choose to leave program --- Subnetting Calculator/SubnetCalc.py | 111 +++++++++++++++------------- 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py index 693749f7..3f5400f1 100644 --- a/Subnetting Calculator/SubnetCalc.py +++ b/Subnetting Calculator/SubnetCalc.py @@ -11,62 +11,67 @@ def formatToAddress(ip: list): while True: - ipAddress = input("Give a valid IPv4 address: ") - ipAddress = ipAddress.replace(" ", "") - subnetMask = input("Give a valid Subnet Mask: ") - subnetMask = subnetMask.replace(" ", "") + 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") + 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 \".\"") + 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" + 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) + 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 + firstUsableAddress = networkAddress.copy() + firstUsableAddress[3] += 1 + lastUsableAddress = broadcastAddress.copy() + lastUsableAddress[3] -= 1 -print("_____________________________________________\n") -print("IPv4 Address: "+ipAddress) -print("Subnet Mask: "+subnetMask) -print("Range of IPs: "+formatToAddress(networkAddress) + - " - "+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)) + print("_____________________________________________\n") + print("IPv4 Address: "+ipAddress) + print("Subnet Mask: "+subnetMask) + print("Range of IPs: "+formatToAddress(networkAddress) + + " - "+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 + From 794dfe845742ebe2369d2226649eccbb99218f09 Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:12:21 +0000 Subject: [PATCH 07/10] feat(SubnetCalc): Reformat results --- Subnetting Calculator/SubnetCalc.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py index 3f5400f1..aabf5850 100644 --- a/Subnetting Calculator/SubnetCalc.py +++ b/Subnetting Calculator/SubnetCalc.py @@ -61,16 +61,16 @@ def formatToAddress(ip: list): lastUsableAddress[3] -= 1 print("_____________________________________________\n") - print("IPv4 Address: "+ipAddress) - print("Subnet Mask: "+subnetMask) - print("Range of IPs: "+formatToAddress(networkAddress) + + print("IPv4 Address: | "+ipAddress) + print("Subnet Mask: | "+subnetMask) + print("Range of IPs: | "+formatToAddress(networkAddress) + " - "+formatToAddress(broadcastAddress)) - print("Range of Usable Host IPs: "+formatToAddress(firstUsableAddress) + + 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)) + 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 From 66d9733770830b3196c26e93a497e1dc183ab80f Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Sat, 14 Oct 2023 18:19:47 +0000 Subject: [PATCH 08/10] feat: added network & broadcast address - Got rid of range of IPs b/c it's not very useful --- Subnetting Calculator/SubnetCalc.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Subnetting Calculator/SubnetCalc.py b/Subnetting Calculator/SubnetCalc.py index aabf5850..e77911e7 100644 --- a/Subnetting Calculator/SubnetCalc.py +++ b/Subnetting Calculator/SubnetCalc.py @@ -3,10 +3,9 @@ def formatToAddress(ip: list): ipString = "" - for i in range(4): - ipString += str(ip[i])+"." + for num in range(4): + ipString += str(ip[num])+"." ipString = ipString[:-1] - return ipString @@ -63,15 +62,14 @@ def formatToAddress(ip: list): print("_____________________________________________\n") print("IPv4 Address: | "+ipAddress) print("Subnet Mask: | "+subnetMask) - print("Range of IPs: | "+formatToAddress(networkAddress) + - " - "+formatToAddress(broadcastAddress)) + print("Network Address: | "+formatToAddress(networkAddress)) + print("Broadcast Address: | "+formatToAddress(broadcastAddress)) print("Range of Usable Host IPs: | "+formatToAddress(firstUsableAddress) + - " - "+formatToAddress(lastUsableAddress)) + " - "+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"): + if (input("\nDo you want to exit this program? (y/n) ") == "y"): break - From 4d55df9d545c341402812a648859f03880392c1c Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Sat, 14 Oct 2023 11:29:48 -0700 Subject: [PATCH 09/10] feat(SubnetCalc): created README.md --- Subnetting Calculator/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Subnetting Calculator/README.md 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 From 15e948f3414fc3e61885858b006769f9c4e45bba Mon Sep 17 00:00:00 2001 From: 101zh <67253838+101zh@users.noreply.github.com> Date: Sun, 15 Oct 2023 10:41:19 -0700 Subject: [PATCH 10/10] feat(SubnetCalc): added Subnetting Calculator to table of contents --- README.md | 1 + 1 file changed, 1 insertion(+) 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 |