How to Build a Ping Sweeper in Python

2022-09-10 04:14:47 By : Mr. Yong Bai

Build this useful and informative utility with barely a handful of lines of 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.

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.

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.

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:

Now that you have a clear picture of the workflow let's begin programming.

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:

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.

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.

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.

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.

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.

Debarshi Das is an independent security researcher with a passion for writing about cybersecurity and Linux. With over half a decade of experience as an online tech and security journalist, he enjoys covering news and crafting simplified, highly accessible explainers and how-to guides that make tech easier for everyone. While he's programming and publishing by day, you'll find Debarshi hacking and researching at night.

Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!