Write a Ping Sweeper in 4 Lines of Bash

What we will do today is write a script that will ping a certain IP range and tell us which IP’s respond to our ICMP requests. In other words we will be writing a ping sweeper. So let’s get started.

We can do this easily by using a for loop to iterate through a given IP range and then for each IP execute the ping command with the IP as the argument.

Normally when we ping a host in Linux it just keeps on pinging indefinitely.


So we will need to figure out how to limit the amount of pings to each host.

A quick peak at the man pages ( man ping ) for the ping command tells us that -c parameter and the number 1 would just send 1 ping command to each host. This is what we are after.


Firstly we will need to open a shell and then use a text editor to write the script. We will be using nano as our text editor. So let’s create the file on the command line with the command:

nano ping-loop.sh


We will start with shebang followed by a for loop and use the sequence command for the IP range.

#! /bin/bash

for ip in $(seq 200 210); do

echo 192.168.31.$ip


So firstly we will echo out the results just to see if there is any mistakes in the code. So let’s save the file, exit the editor (CNTRL x, CNTRL y)and give it executable permissions (chmod 755 ping-loop.sh).

and run the script



Excellent, so far the script echoes out the range of IP’s we specified in the code. Let’s continue editing this script. Now that we have our IP ranges we can now add the ping command to the loop and specify the amount of pings per host.

#! /bin/bash

for ip in $(seq 200 210); do

ping -c 1 192.168.31.$ip


We just removed the echo command and replaced it with the ping command and included the -c parameter and 1 for the amount of pings to each host. Let’s run this new script.


It worked!! but it took a little long to complete and provided us with a messy output. So let’s deal with the output first. By looking at the output above we could possibly grep for the ‘bytes from’ string. We could also use the cut command and use space as a delimiter and then display the fourth field in the IP address. So let’s include these output filters in our script.

#! /bin/bash

for ip in $(seq 200 210); do

ping -c 1 192.168.31.$ip | grep "bytes from" | cut -d " " -f  4 | cut -d ":" -f  1


That looks good, let’s try running our script once again.


So the output has improved but the speed has not, so let’s edit the script to make it run a bit faster.

The reason it took so long is because it was pinging each IP in sequence.

We can use a cool trick to speed the process up. We can make the ping command in our loop to run in the background. This way, one ping command will not complete before the other ping command is issued. The code is as follows.

#! /bin/bash

for ip in $(seq 200 210); do

ping -c 1 192.168.31.$ip | grep "bytes from" | cut -d " " -f  4 | cut -d ":" -f  1 &


All we did here was add an & at the end of the loop. This took half the time!!

That’s it 🙂 thanks for taking the time to read this article, be sure to follow, like and share, thanks in advance.

QuBits 2019-08-21



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.