-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |