From 40ed90849e45c622b4706de446f570c497f2dac3 Mon Sep 17 00:00:00 2001 From: Chandan Prajwal <76419264+ChandanSouL@users.noreply.github.com> Date: Sun, 1 Sep 2024 04:01:21 +0530 Subject: [PATCH 1/3] Subnet Calculator for Local-Area-Network(LAN) --- Subnet-Calculator/requirements.txt | 1 + Subnet-Calculator/subnet_calculator.py | 38 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 Subnet-Calculator/requirements.txt create mode 100644 Subnet-Calculator/subnet_calculator.py diff --git a/Subnet-Calculator/requirements.txt b/Subnet-Calculator/requirements.txt new file mode 100644 index 0000000..0b574b5 --- /dev/null +++ b/Subnet-Calculator/requirements.txt @@ -0,0 +1 @@ +psutil \ No newline at end of file diff --git a/Subnet-Calculator/subnet_calculator.py b/Subnet-Calculator/subnet_calculator.py new file mode 100644 index 0000000..d0935bd --- /dev/null +++ b/Subnet-Calculator/subnet_calculator.py @@ -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() From eab6986833273a8846316049a1c0780fdc8275b6 Mon Sep 17 00:00:00 2001 From: Chandan Prajwal <76419264+ChandanSouL@users.noreply.github.com> Date: Sun, 1 Sep 2024 04:38:00 +0530 Subject: [PATCH 2/3] Adding a readmefile --- Subnet-Calculator/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Subnet-Calculator/README.md diff --git a/Subnet-Calculator/README.md b/Subnet-Calculator/README.md new file mode 100644 index 0000000..a33f63b --- /dev/null +++ b/Subnet-Calculator/README.md @@ -0,0 +1,16 @@ +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 + + + + \ No newline at end of file From 92dcd9bdbf3ad08dfc5e91f1614f5640f9751191 Mon Sep 17 00:00:00 2001 From: Chandan Prajwal <76419264+ChandanSouL@users.noreply.github.com> Date: Sun, 1 Sep 2024 04:39:44 +0530 Subject: [PATCH 3/3] Adding readme file --- Subnet-Calculator/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Subnet-Calculator/README.md b/Subnet-Calculator/README.md index a33f63b..2f81119 100644 --- a/Subnet-Calculator/README.md +++ b/Subnet-Calculator/README.md @@ -11,6 +11,11 @@ Incase you have anything to be followed while executing the python script mentio 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 + \ No newline at end of file