Skip to content

Commit c20547e

Browse files
authored
Subnetting calculator (DhanushNehru#296)
1 parent fc968ff commit c20547e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ More information on contributing and the general code of conduct for discussion
109109
| Snake Water Gun | [Snake Water Gun](https://github.com/DhanushNehru/Python-Scripts/tree/master/Snake%20Water%20Gun) | A game similar to Rock Paper Scissors. |
110110
| Sorting | [Sorting](https://github.com/DhanushNehru/Python-Scripts/tree/master/Sorting) | Algorithm for bubble sorting. |
111111
| Star Pattern | [Star Pattern](https://github.com/DhanushNehru/Python-Scripts/tree/master/Star%20Pattern) | Creates a star pattern pyramid. |
112+
| 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. |
112113
| 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. |
113114
| 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 |
114115
| 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. |

Subnetting Calculator/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Subnetting Calculator
2+
3+
This Python script calculates network information based on a given IP address and subnet mask.
4+
5+
## Features
6+
7+
- Calculates network address and broadcast address
8+
- Provides the range of IP addresses for the network
9+
- Shows the usable host IP range
10+
- Calculates the total number of hosts and usable hosts
11+
12+
## Usage
13+
14+
1. Ensure you have Python 3.x installed on your system.
15+
2. Run the script:
16+
```
17+
python subnetting_calculator.py
18+
```
19+
3. Enter the IP address and subnet mask when prompted.
20+
21+
## Example
22+
23+
```
24+
Enter IP address: 192.168.1.1
25+
Enter subnet mask: 255.255.255.0
26+
27+
Network Information:
28+
IP Address: 192.168.1.1
29+
Subnet Mask: 255.255.255.0
30+
Network Address: 192.168.1.0
31+
Broadcast Address: 192.168.1.255
32+
Range of IP addresses: 192.168.1.0 - 192.168.1.255
33+
Usable Host IP Range: 192.168.1.1 - 192.168.1.254
34+
Total Hosts: 256
35+
Usable Hosts: 254
36+
```
37+
38+
## Requirements
39+
40+
This script uses the `ipaddress` module, which is part of the Python standard library. No additional installations are required.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import ipaddress
2+
3+
4+
def calculate_network_info(ip_address, subnet_mask):
5+
network = ipaddress.IPv4Network(f"{ip_address}/{subnet_mask}", strict=False)
6+
7+
network_address = network.network_address
8+
broadcast_address = network.broadcast_address
9+
total_hosts = network.num_addresses
10+
usable_hosts = max(total_hosts - 2, 0)
11+
first_usable_host = network_address + 1 if usable_hosts > 0 else None
12+
last_usable_host = broadcast_address - 1 if usable_hosts > 0 else None
13+
14+
results = {
15+
"IP Address": ip_address,
16+
"Subnet Mask": subnet_mask,
17+
"Network Address": str(network_address),
18+
"Broadcast Address": str(broadcast_address),
19+
"Range of IP addresses": f"{network_address} - {broadcast_address}",
20+
"Usable Host IP Range": (
21+
f"{first_usable_host} - {last_usable_host}" if usable_hosts > 0 else "N/A"
22+
),
23+
"Total Hosts": total_hosts,
24+
"Usable Hosts": usable_hosts,
25+
}
26+
27+
return results
28+
29+
30+
def main():
31+
try:
32+
ip_address = input("Enter IP address: ")
33+
subnet_mask = input("Enter subnet mask: ")
34+
35+
info = calculate_network_info(ip_address, subnet_mask)
36+
37+
print("\nNetwork Information:")
38+
for key, value in info.items():
39+
print(f"{key}: {value}")
40+
41+
except ValueError as e:
42+
print(f"Error: {e}")
43+
44+
45+
if __name__ == "__main__":
46+
main()

0 commit comments

Comments
 (0)