Tuesday, July 26, 2016

How To Ping IP Range Shell Script

Overview


At times we have to run a network test for available IP Addresses or IP Address that are pinging. This script will help us in identifying the hosts that are reachable or unreachable.

Applies To

·        Tested on CentOS 7

Pre-requisites

Bash

Shell Script – Snippet

Copy and paste the script into a file, change script permission and run it.


#!/bin/bash

# To debug uncomment the below line

#set -x

# To store the result to a output file

OutputFileName=IP_Ping_Result.txt

# Check if the Output file exists and delete if the file already exists

if [ -f ${OutputFileName} ]; then
               echo  "${OutputFileName} exists.."
               echo "Removing existing file..."
               rm     ${OutputFileName}
               echo -e "Removed ${OutputFileName} file...\n\a"
else
               echo  -e "${OutputFileName} not found.. \n\a"
fi

# Loop through IP Range

for IPAddr in 192.168.1.{1..254}; do
               echo "Pinging ${IPAddr}..."
               echo "pinging ${IPAddr} now..." >> ${OutputFileName}
               ping -c 1 -w 5 $IPAddr | grep "bytes from" >> ${OutputFileName}
done

echo "IP ping test completed. Result stored in ${OutputFileName}"

Run Ping IP Range - Shell Script

To run the script, ensure you change the file permission to “executable”, run command;

chmod +x ./Ping.sh

./Ping.sh


Ping Result Output File

To view the results of the ping range script.


cat IP_Ping_Result.txt


Slideshare Information

A downloadable document has been uploaded to Slideshare.

No comments:

Post a Comment