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.

pings1

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.

pings2.PNG

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

pings3

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

done

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

./ping-loop.sh.

pings4

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

done

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.

pings5

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

done

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

pings6

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 &

done

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 comment

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