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 applications. If you’re new to Python, there’s no better way to reinforce your learning than by working on mini-projects.

A great example of a Python project to write is a ping sweep, 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 sweep is a program that accepts a network address as input, pings hosts on the network, and displays a list of dead and active hosts. It’s a simple way to estimate the number of online hosts on your network and find out their IPv4 addresses.

As a beginner, creating a ping sweep is a great way to brush up on your Python basics. This hands-on mini-project will also refresh your understanding of networking fundamentals.

Requirements for your Ping Sweeper

Before starting the development process, you need to make sure that 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 pressing Walk in:

python --version

On some systems you may need to run:

python3 --version

This command should return the version of Python installed on your system. If it returns an error similar to “python not found”, you need to install Python3 and then proceed with the following steps.

Code the Ping Sweeper

There are several approaches to creating this script. Some would require you to install and import multiple modules. Here you will take a minimalist approach that has no external dependencies other than the crucial elements operating system module.

Before you start coding, break down the requirements to better understand the features you’ll need to implement. There are three parts to this script:

  1. Accept the input IP address.
  2. Extract the network ID from the IP address.
  3. Iterate over all hosts on the network and print if a host is dead or alive.

Now that you have a clear picture of the workflow, let’s start programming.

Acceptance and processing of entry

The first part of the script deals with accepting user input and reducing that IPv4 address to its first three octets. This gives us the network ID:

import os

IP = input("[+] Enter the Host IP Address:t")
print("[+] Starting Ping Sweeper on " + IP)
dot = IP.rfind(".")
IP = IP[0:dot + 1]

The to input() the function accepts input from the user. You can use a string to find() method to extract the index of the last occurrence of the decimal point and store it in the point variable. Follow it, retaining everything from the entry to the rightmost occurrence of a decimal point.

Host scan and host status print

You derived the network address from the input IP address. You can now cycle through all possible values ​​for the final IPv4 byte: 1–254. Inside the for loop, store the new IP address in the host variable. This IP address is the base IP address followed by the value of the iterator variable. Then use the os.system() method to perform 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")
else:
print(host + " is down")

Test it answer value against 0 to determine the state of the host and decide if it is 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 vs flag and w flag, with values ​​of 1, to the original ping command. This causes it to send a single packet and wait a second to receive a response. Your version of ping may or may not support these options; check the ping man page to verify.

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 only compatible with Unix or Linux systems. You can run this script on Windows by replacing the vs flag with not and >/dev/null with >null.

Running the Ping Sweeper script

You can run this script in the terminal or through a command prompt. Launch a terminal, navigate to the location of the script and run it with python3:

cd /directory/sweeper/
python3 sweeper.py

Enter an IPv4 address or 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 will understand concepts, develop critical skills, and understand how to solve problems.

If you’ve run out of project ideas to work on, check out this curated list of the best project ideas for Python.

About Jon Moses

Check Also

Check department, course name and other details here

NSIC Recruitment 2022: Check Department, Course Name and Other Details Here 2022 NSIC recruitment: National …