Date created: Thursday, March 25, 2010 8:36:15 PM. Last modified: Thursday, April 13, 2023 11:47:27 AM

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

 

Ping with timestamps:


#!/bin/bash

if [[ $1 && ${1-x} ]]
then
ip=$1
else
ip="192.168.0.1"
fi

echo "Pinging $ip"

ping "$ip" | while read pong; do echo "$(date): $pong" | tee -a ping_$ip.log; done

 

ARPing with timestamps:


#!/bin/bash

if [[ $1 && ${1-x} ]]
then
ip=$1
else
ip="192.168.0.1"
fi

echo "ARPing $ip"

# Mac OS X requires "brew install arping":
/usr/local/Cellar/arping/2.20/sbin/arping -i en0 "$ip" | while read arpong; do echo "$(date): $arpong" | tee -a arping_$ip.log; done

 


Previous page: PeeringDB API Examples
Next page: RANCID Filter