How to Build a Ping Sweeper in Python

Python is a versatile programming language that you can use to write various types of GUI and CLI-based applications. If you’re new to Python, there’s no better way to deepen your learning than working on mini-projects.

A great example Python project to write is a ping sweeper, a small utility that probes network hosts. This script covers basic programming concepts, including print statements, loops, and functions.

What is a ping sweeper?

A ping sweeper is a program that takes a network address as input, pings hosts on the network and prints the list of dead and active hosts. It’s an easy way to estimate the number of online hosts on your network and find out their IPv4 addresses.

As a beginner, building a ping sweeper is a great way to brush up on your Python basics. This handy mini-project will also refresh your understanding of networking basics.

Requirements for your ping sweeper

Before starting the development process, you should make sure you have the latest version of Python on your system.

Read  Liz Weston: How to cultivate a happier retirement

Verify that you can run Python by typing this command in Command Prompt (for Windows users) or Terminal (for UNIX/Linux systems) and pressing Enter:

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 should install Python3 and then continue with the following steps.

Encoding of the ping sweeper

There are several approaches to creating this script. Some require you to install and import multiple modules. Here you choose a minimalist approach that has no external dependencies other than the crucial ones os module.

Before you start coding, break down the requirements to better understand what features you need to implement. This script consists of three parts:

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

Now that you have a clear picture of the workflow, we can start 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:

import os

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

That Entry() Function accepts user input. You can use a string find() Method to extract and store the index of the last occurrence of the decimal point in the Point Variable. Follow it, keeping everything from the input to the rightmost occurrence of a decimal point.

Read  How to keep electricity sustainable and affordable

Scan hosts and print host status

You have derived the network address from the entered IP. You can now iterate over all possible values ​​for the last IPv4 octet: 1-254. Inside the for loop, save 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 of running the Ring command against them 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 them out answer Value approaches 0 to determine the status of the host and decide whether it is online or offline. If ping encounters an unresponsive host, it returns a non-zero value. Otherwise, it returns null to indicate a host it can reach.

You can add those c flag & w Flag with values ​​from 1 to the original ping command. As a result, it just sends a packet and waits a second to get a response. Your version of ping may or may not support these options; Consult the ping man page to verify this.

You should also redirect the output to /dev/null to hide the details 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 using the c flag with n and >/dev/null With >zero.

Read  How to Make $165 per Month in Passive Income Right Now

Running the ping sweeper script

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

cd /directory/sweeper/

Enter an IPv4 address or subnet into the terminal and the ping sweeper should get to work, returning 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 better you understand concepts, build fundamental skills, and understand how to solve problems.

If you’re running out of project ideas to work on, check out this curated list of the best Python project ideas.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button