ping

Simple ping sweeper with Bash

This is a piece of simple code demostrating how to do a ping sweeper using only Bash and ping, which are largely available in most Linux distro. It will ping and write online host IP address to a text file named “ping_result”.

Beware, it will delete the old file “ping_result” before doing the ping. So, it is best that you run it in an empty directory to prevent the risk to losing your file.

This simple piece of code will ping from 192.168.188.125 to 192.168.189.130. You should change it if you want to ping other addresses.

Code:


#!/bin/bash

echo "Save to ping_result file in the current directory"

#Check if ping_result file exists
#If it does, delete it to make way for the new file
if test -e ping_result
then
rm ping_result
echo "File exist. Delete old file. Create new file"
fi

for x in {188..189} #=>range from 188 to 189
do
for y in {125..130} #range from 125 to 130
do
#ping on 192.168.188.125 to 192.168.189.13 #write result to ping_result file
ping -c 1 192.168.$x.$y|grep "from"|cut -d " " -f 4| cut -d ":" -f 1>>ping_result
done
done