Skip to content

Commit

Permalink
Subnetting calculator (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivnvxd authored Oct 5, 2024
1 parent fc968ff commit c20547e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,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. |
Expand Down
40 changes: 40 additions & 0 deletions Subnetting Calculator/README.md
Original file line number Diff line number Diff line change
@@ -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.
46 changes: 46 additions & 0 deletions Subnetting Calculator/subnetting_calculator.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c20547e

Please sign in to comment.