In this script, we see the use of several powerful libraries to accomplish a simple task: scanning a network for active hosts. We’ll take a look at a Python script that makes it easy to scan a network and find active hosts.
The script begins by importing several libraries that will be used throughout the script. The first is the concurrent.futures
library that provides an easy way to parallelize tasks in Python. This is important for the script because we want to simultaneously ping multiple IP addresses in order to quickly find active hosts on the network.
import concurrent.futures from tqdm import tqdm import subprocess from prettytable import PrettyTable import socket import ipaddress
Another library that we import is tqdm
. This library adds a progress bar to our script, which is a nice visual aid that lets us know how much of the network scan has been completed. This is especially helpful when scanning large networks as it gives an idea of how much time is left for the scan to complete.
We also import the subprocess
library, which allows us to run command-line commands from within Python. In this script, we use the subprocess
library to run the ping
command, which is used to check if a host is active.
def ping_host(ip): try: subprocess.check_output("ping -c 1 " + ip, shell=True) return ip except: pass
Additionally, the PrettyTable
library is used to display the results of our scan in a neat, tabular format.
Once the necessary libraries have been imported, the script prompts the user to either scan their current network or enter a specific network address. If the user chooses to scan their current network, the script uses the socket
library to determine the user’s IP address and subnet mask. If the user chooses to enter a specific network address, they are prompted to do so in the format x.x.x.x/24.
def scan_network(): choice = input("Do you want to scan your current network or enter a specific network address? (1 for Current/2 for specific)") if choice == "1": hostname = socket.gethostbyname(socket.gethostname()) ip = ipaddress.ip_address(hostname) subnet = ipaddress.ip_interface(f"{ip}/24") ip_range = str(subnet.network) subnet_mask = str(subnet.netmask) print(f"Detected {ip_range} as your network range and {subnet_mask} as your subnet mask\n Scanning Now") elif choice == "2": ip_range = input("Enter the network address in the format x.x.x.x/24: ") else: print("Invalid choice. Exiting...") return
With the network range determined, the script uses the concurrent.futures
library to simultaneously ping all IP addresses in that range. As each ping completes, the tqdm
library updates the progress bar to show the current status of the scan. This allows us to quickly scan the entire network and find active hosts without having to wait for each ping to complete.
active_hosts = [] with concurrent.futures.ThreadPoolExecutor() as executor: futures = [executor.submit(ping_host, ip_range[:-3] + str(i)) for i in range(1,256)] for future in tqdm(concurrent.futures.as_completed(futures), total=256, desc="Scanning network"): ip = future.result() if ip: active_hosts.append(ip)
Once all IP addresses have been checked, the script uses the PrettyTable
library to display a list of all active hosts. This makes it easy to read and understand the results of the scan. If no active hosts are found, the script will display a message to that effect.
if active_hosts: table = PrettyTable() table.field_names = ["Active Hosts"] for host in active_hosts: table.add_row([host]) print(table) else: print("No active hosts found.")
This script is a great example of how a few simple libraries can be used to accomplish a useful task with very little code. The use of parallelization and a progress bar make the script efficient and user-friendly, while the PrettyTable
library makes the results easy to read.
The entire code can be found here on my GitHub.
It is important to note that this script is just a basic tool for scanning a network, as it uses the basic ping command to check if a host is active. In real-world scenarios, more advanced tools and techniques are required to properly scan a network. This script is intended to be used as a starting point to understand the basics of network scanning and how to accomplish it using Python.
Leave a Reply