diff --git a/README.md b/README.md index 30e73edd..80a0ddcf 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,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%20Water%20Gun) | 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) | Calculates network information based on a given IP address and subnet mask. | | 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 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 | | Text to Image | [Text to Image](https://github.com/DhanushNehru/Python-Scripts/tree/master/Text%20to%20Image) | A Python script that will take your text and convert it to a JPEG. | diff --git a/Subnetting Calculator/README.md b/Subnetting Calculator/README.md new file mode 100644 index 00000000..2fd34271 --- /dev/null +++ b/Subnetting Calculator/README.md @@ -0,0 +1,40 @@ +# Subnetting Calculator + +This Python script calculates network information based on a given IP address and subnet mask. + +## Features + +- Calculates network address and broadcast address +- Provides the range of IP addresses for the network +- Shows the usable host IP range +- Calculates the total number of hosts and usable hosts + +## Usage + +1. Ensure you have Python 3.x installed on your system. +2. Run the script: + ``` + python subnetting_calculator.py + ``` +3. Enter the IP address and subnet mask when prompted. + +## Example + +``` +Enter IP address: 192.168.1.1 +Enter subnet mask: 255.255.255.0 + +Network Information: +IP Address: 192.168.1.1 +Subnet Mask: 255.255.255.0 +Network Address: 192.168.1.0 +Broadcast Address: 192.168.1.255 +Range of IP addresses: 192.168.1.0 - 192.168.1.255 +Usable Host IP Range: 192.168.1.1 - 192.168.1.254 +Total Hosts: 256 +Usable Hosts: 254 +``` + +## Requirements + +This script uses the `ipaddress` module, which is part of the Python standard library. No additional installations are required. diff --git a/Subnetting Calculator/subnetting_calculator.py b/Subnetting Calculator/subnetting_calculator.py new file mode 100644 index 00000000..c9638fbf --- /dev/null +++ b/Subnetting Calculator/subnetting_calculator.py @@ -0,0 +1,46 @@ +import ipaddress + + +def calculate_network_info(ip_address, subnet_mask): + network = ipaddress.IPv4Network(f"{ip_address}/{subnet_mask}", strict=False) + + network_address = network.network_address + broadcast_address = network.broadcast_address + total_hosts = network.num_addresses + usable_hosts = max(total_hosts - 2, 0) + first_usable_host = network_address + 1 if usable_hosts > 0 else None + last_usable_host = broadcast_address - 1 if usable_hosts > 0 else None + + results = { + "IP Address": ip_address, + "Subnet Mask": subnet_mask, + "Network Address": str(network_address), + "Broadcast Address": str(broadcast_address), + "Range of IP addresses": f"{network_address} - {broadcast_address}", + "Usable Host IP Range": ( + f"{first_usable_host} - {last_usable_host}" if usable_hosts > 0 else "N/A" + ), + "Total Hosts": total_hosts, + "Usable Hosts": usable_hosts, + } + + return results + + +def main(): + try: + ip_address = input("Enter IP address: ") + subnet_mask = input("Enter subnet mask: ") + + info = calculate_network_info(ip_address, subnet_mask) + + print("\nNetwork Information:") + for key, value in info.items(): + print(f"{key}: {value}") + + except ValueError as e: + print(f"Error: {e}") + + +if __name__ == "__main__": + main()