Posted in Penetration Testing, Scripting

BASH ping sweeper

I had a little problem that was annoying me for a while. I wanted to learn BASH scripting, however, I didn’t really have an idea or a problem to solve using it. I’ve written small BASH scripts before to update things like Kali.

I use nmap a lot, so I wanted to use BASH as a way to prompt for a user input, save to a file and let nmap use that data to perform a ping sweep of the given data and return the results as clean IP address of all active hosts to the terminal.

Below is the source code I wrote for this task. Ping sweeping isn’t something probably requires a complex BASH script, however, I wanted to learn it, and it bugged me. It’s open to a lot of uses so I may develop it further.


touch hostlist.txt
echo ">> Please enter IP/range"
   read ip_input
echo $ip_input > hostlist.txt
echo "$(tput setaf 3)[+]$(tpu sgr0) Running ping sweep on $ip_input"
   nmap -sP -iL hostlist.txt -oG pingscan > /dev/null
   grep Up pingscan | awk '{print$2}' > /root/Desktop/SCanResult/uplist
   grep Down pingscan | awk '{print$2}' > /root/Desktop/ScanResult/downlist
   cat /root/Desktop/ScanResult/uplist
echo "$(tput setaf 2)[+]$(tput sgr0)Hosts that are up from '$ip_input'"
  rm hostlist.txt

I plan to check for the hostlist.txt file on startup using some sort of if statement. If the file exists, then overwrite the file, else create it. I’m also not sure about how I want deal with the hostfile.txt. I’ve set it in the code to delete it once we’ve used it, but it may be needed later for documentation.


The exercise was one to solve an Input, Process & Output problem with minimal input.


Thanks for reading and feel free to use the code.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s