Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subnet Calculator for Local-Area-Network(LAN) #264

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Subnet-Calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This repository consists of a list of python scripts to automate few tasks.

You can contribute by adding more python scripts which can be used to automate things. Some of already done are listed below.
Incase you have anything to be followed while executing the python script mention it as well


# Python Script

## Script - Subnet Calculator

Python code to Identify your IP addresses, Subnet, Broadcast channel of all you LAN interfaces.
subnet_calculator.py

# How to Use
pip install psutil / pip install requirements.txt

python3 subnet_calculator.py


<!-- Updated README links and corrected typos -->
<!-- Updated README links and corrected typos -->
1 change: 1 addition & 0 deletions Subnet-Calculator/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psutil
38 changes: 38 additions & 0 deletions Subnet-Calculator/subnet_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import psutil
import ipaddress

def get_network_info():
network_info = {}
for interface, addrs in psutil.net_if_addrs().items():
for addr in addrs:
# 2 represents IPv4
if addr.family == 2:
# Collect information only for IPv4 addresses
network_info[interface] = (addr.address, addr.netmask, addr.broadcast)
return network_info

def calculate_subnet_details(ip, netmask):
# Convert IP and netmask to ipaddress objects
network = ipaddress.ip_network(f"{ip}/{netmask}", strict=False)
subnet = network.network_address
broadcast = network.broadcast_address
first_usable_ip = subnet + 1
last_usable_ip = broadcast - 1
return subnet, broadcast, first_usable_ip, last_usable_ip

def main():
network_info = get_network_info()

for interface, (ip, netmask, broadcast) in network_info.items():
subnet, broadcast, first_usable_ip, last_usable_ip = calculate_subnet_details(ip, netmask)
print(f"Interface: {interface}")
print(f"IP Address: {ip}")
print(f"Subnet: {subnet}")
print(f"Netmask: {netmask}")
print(f"Range of IP addresses: {subnet} - {broadcast}")
print(f"Usable Range of IPs: {first_usable_ip} - {last_usable_ip}")
print(f"Broadcast: {broadcast}")
print()

if __name__ == "__main__":
main()