Date created: 03/25/10 20:36:15. Last modified: 03/26/17 11:34:10

Ping Scripts

This script pings a list of IP addresses (space seperated) and pings them the number of times, as per the PCount variable;

#!/bin/bash

# Space seperated IPs
IPS="172.16.0.10 172.16.0.253 172.16.0.1 192.168.1.1"

# Number of times to ping each IP
PCount=4

# Loop round each space separated value in $IPS
for hosts in $IPS
do
	# Count the number of returned ping replies by grepping the results lines and counting them
	count=$(ping -A -c $PCount -i 0.2 -W 1 $hosts | grep -c "64 bytes")
	echo "Host $hosts returned $count out of $PCount"
done

This next script pings a class C range of address (or subnet of a C range). Call the script as './pingc.sh 192.168.1 1 254' this will ping every address from 192.168.1.1 to 192.168.1.254:

#!/bin/bash

for ((i=$2;i<=$3;i++))
do
	host=$1.$i
	PCount=4
	count=$(ping -A -c $PCount -i 0.2 -W 1 $host | grep -c "64 bytes")
	echo "Host $host returned $count out of $PCount"
done

This script will ping essentialy B class ranges, its easier than calling the above script multiple times to ping say 172.16.0.1 to 172.16.10.254. I call it with './pingb.sh 172.16 0 10'

#!/bin/bash

for ((b=$2;b<=$3;b++))
do
	for ((c=1;c<=254;c++))
	do
		host=$1.$b.$c
		PCount=4
		count=$(ping -A -c $PCount -i 0.2 -W 1 $host | grep -c "64 bytes")
		echo "Host $host returned $count out of $PCount"
	done
done

Previous page: Multiple IP Pinger
Next page: RANCID Filter