From 713fc747c16ab453344248a256b39663c290c306 Mon Sep 17 00:00:00 2001 From: Jayaditya Dev <114943741+jayadityadev@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:41:11 +0530 Subject: [PATCH] Network Scripts directory added (#611) Added pingsweep-network-scanner in Network_Scripts/. Signed-off-by: Jayaditya Dev <114943741+jayadityadev@users.noreply.github.com> --- .../pingsweep_network_scanner/README.md | 66 +++++++++++++++++++ .../pingsweep_network_scanner/functions.py | 42 ++++++++++++ .../pingsweep_network_scanner/main.py | 16 +++++ 3 files changed, 124 insertions(+) create mode 100644 Network_Scripts/pingsweep_network_scanner/README.md create mode 100644 Network_Scripts/pingsweep_network_scanner/functions.py create mode 100644 Network_Scripts/pingsweep_network_scanner/main.py diff --git a/Network_Scripts/pingsweep_network_scanner/README.md b/Network_Scripts/pingsweep_network_scanner/README.md new file mode 100644 index 00000000..5c027064 --- /dev/null +++ b/Network_Scripts/pingsweep_network_scanner/README.md @@ -0,0 +1,66 @@ +# Ping Sweep Network Scanner + +## Table of Contents + +- [Overview](#overview) +- [Features](#features) +- [Usage](#usage) +- [Example](#example) + +## Overview + +This Python script allows you to perform a ping sweep on a range of IP addresses within a specified subnet to identify live hosts. It supports both Windows and Unix-like systems for the ping command. + +## Features + +- Scan a range of IP addresses within a subnet. +- Detect live hosts within the specified range. +- Cross-platform compatibility (Windows and Unix-like systems). + +## Usage + +1. Run the program + + * For Windows (Powershell/CMD): + + ``` + python main.py + ``` + + * For Linux (bash/zsh/unix): + + ```bash + sudo python3 main.py + ``` + + `Root privilege is required for linux users, as modern kernels of linux don't allow pinging without root privilege.` + +2. Follow the on-screen instructions to provide the following information: + + * Subnet IP (e.g., 192.168.0) + * Starting IP range + * Ending IP range + +## Example + +``` +Enter the SUBNET IP: 192.168.1 +Enter Starting Range: 1 +Enter Ending Range: 10 +``` +The script will scan the hosts ranging from IP Address: 192.168.1.1 to 192.168.1.10. + +``` +Scanning completed in 0:00:13.741418 + +Live Hosts: +192.168.1.1 --> Live +192.168.1.2 --> Live +192.168.1.5 --> Live +192.168.1.7 --> Live +192.168.1.9 --> Live +``` + +--- + +`The efficiency of the program solely depends on factors such as network traffic and connectivity of devices to the router/dhcp server. Also, in some cases, the program skips some live hosts, even though they're up due to delay in responding to the ping command within the time slot.` diff --git a/Network_Scripts/pingsweep_network_scanner/functions.py b/Network_Scripts/pingsweep_network_scanner/functions.py new file mode 100644 index 00000000..ef878d28 --- /dev/null +++ b/Network_Scripts/pingsweep_network_scanner/functions.py @@ -0,0 +1,42 @@ +import platform +from datetime import datetime +import subprocess + + +def instructions(): + print(""" + 1. Enter an IP range for the ping sweep. + 2. Enter the range to start ping from. + 3. Enter the range to end ping at. + + Example: -192.168.0 + -2 + -8 + Script will scan from 192.168.0.2 to 192.168.0.8 \n""") + + +def network_info(): + subnet_ip = input('Enter the SUBNET IP: ') + first_host = int(input('Enter Starting Range: ')) + last_host = int(input('Enter Ending Range: ')) + 1 + return subnet_ip, first_host, last_host + + +def network_scan(): + subnet_ip, first_host, last_host = network_info() + time1 = datetime.now() + live_hosts = [] + + for ip in range(first_host, last_host): + addr = f"{subnet_ip}.{ip}" + command = f"ping -n 1 -w 2500 {addr}" if platform.system() == 'Windows' else f"ping -c 1 -W 2 {addr}" + response = subprocess.run(command, capture_output=True, text=True, shell=True) + + if "TTL" in response.stdout or "ttl" in response.stdout: + live_hosts.append((addr, 'Live')) + + time2 = datetime.now() + total = time2 - time1 + print("\nScanning completed in", total) + + return live_hosts diff --git a/Network_Scripts/pingsweep_network_scanner/main.py b/Network_Scripts/pingsweep_network_scanner/main.py new file mode 100644 index 00000000..3c17b5a0 --- /dev/null +++ b/Network_Scripts/pingsweep_network_scanner/main.py @@ -0,0 +1,16 @@ +import functions + + +def main(): + live_hosts = functions.network_scan() + if live_hosts: + print("\nLive Hosts:") + for host, status in live_hosts: + print(f"{host} --> {status}") + else: + print("\nNo devices up and running in the given range of network.") + + +if __name__ == "__main__": + functions.instructions() + main()