How to Build a Ping Sweeper in Python
Python is a versatile programming language that you can use to write different types of GUI and CLI-based applications. If you’re new to Python, there’s no better way to reinforce your learning than by working on mini-projects.
A great sample Python project to write is a ping sweeper, a small utility that inspects network hosts. This script will cover fundamental programming concepts, including print statements, loops, and functions.
What Is a Ping Sweeper?
A ping sweeper is a program that accepts a network address as input, pings the hosts in the network, and outputs the list of dead and alive hosts. It is an easy way to estimate the number of online hosts in your network and find out their IPv4 addresses.
As a beginner, creating a ping sweeper is a great way to brush up on your Python basics. This hands-on mini-project will also refresh your understanding of networking fundamentals.
Prerequisites for Your Ping Sweeper
Before beginning the development process, you should make sure you have the latest version of Python on your system.
Check if you can run Python by typing this command in the command prompt (for Windows users) or terminal (for UNIX/Linux systems) and hitting Enter:
On some systems, you may need to run:
This command should return the version of Python installed on your system. If it returns an error similar to “python not found”, you should install Python3 and then proceed with the following steps.
Coding the Ping Sweeper
There are multiple approaches to building this script. Some would require you to install and import several modules. Here, you’ll take a minimalist approach that does not have external dependencies other than the crucial os module.
Before you start coding, break down the requirements to better understand what functionality you’ll need to implement. There are three parts to this script:
- Accept the input IP address.
- Extract the network ID from the IP address.
- Iterate over all the hosts in the network and print if a host is dead or alive.
Now that you have a clear picture of the workflow let’s begin programming.
Accepting and Processing the Input
The first part of the script deals with accepting input from the user and reducing that IPv4 address to its first three octets. This gives us the network ID:
IP = input("[+] Enter the Host IP Address:t")
print("[+] Starting Ping Sweeper on " + IP)
dot = IP.rfind(".")
IP = IP[0:dot + 1]
The input() function accepts user input. You can use a string’s rfind() method to extract the index of the last occurrence of the decimal point and store it in the dot variable. Follow it up by retaining everything from the input until the rightmost occurrence of a decimal point.
Scanning the Hosts and Printing Host Status
You have derived the network address from the input IP. You can now iterate through all possible values for the final IPv4 octet: 1–254. Inside the for loop, store the new IP in the host variable. This IP is the base IP followed by the value of the iterator variable. Then, use the os.system() method to run the ping command against the host variable.
for i in range(1, 255):
host = IP + str(i)
response = os.system("ping -c 1 -w 1 " + host + " >/dev/null")
if response == 0:
print(host + " is up")
print(host + " is down")
Test the response value against 0 to determine the host’s status and decide if it’s online or offline. If ping encounters an unresponsive host, it returns a non-zero value. Otherwise, it returns zero to indicate a host it can reach.
You can add the c flag and w flag, with values of 1, to the original ping command. This makes it send only one packet and wait for one second to receive a response. Your version of ping may or may not support these options; consult the ping man page to check.
You should also redirect the output to /dev/null to hide the detail of the ping output. Note that the ping and /dev/null syntax is compatible with only Unix or Linux systems. You can run this script on Windows by replacing the c flag with n and >/dev/null with >nul.
Running the Ping Sweeper Script
You can run this script in the terminal or via a command prompt. Fire up a terminal, move into the location of the script and execute it with python3:
Input an IPv4 address or a subnet into the terminal, and the ping sweeper should get to work and return the expected output.
Interesting Project Ideas for Python
Hands-on learning is arguably the best and fastest way to learn a programming language. The more projects you work on, the more you’ll grasp concepts, build essential skills, and understand how to fix problems.
If you’ve run short of project ideas to work on, check out this curated list of the best project ideas for Python.