Date created: Tuesday, March 6, 2012 10:58:46 PM. Last modified: Thursday, April 13, 2023 11:47:30 AM

Multiple IP Pinger

Ping sweep across single or multiple IPs;

#!/bin/bash
#
# James Bensley - 2012 - jwbensley@gmail.com
# Ping multiple IPs from a file (./iplist) or a single IP specified
# interactively on the CLI. Choose to only display failed pings or
# both successful and failed pings. Defaults:
# 
# Delay between each round of pings: 1 second
# Delay between each individual ping: 1 second
# Echos sent to each IP in each round of pings: 1
# Max echo requests loss threshold: 1
# Display successful pings and failed pings: no

echo "Ping single IP or multiple (assumes ./iplist exists with one IP per line):"
select result in Single Multiple
do
  if [ "$result" == "Multiple" ]
  then

    # Make sure a list file exists
    if [ -e iplist ]
    then

      # And that its not empty
      ips=($(cat iplist))
      if [ ${#ips[@]} -lt 1 ]
      then
        echo "./iplist is empty"
        exit
      fi

    else
      echo "Please create file 'iplist' with one IP per line"
      exit
    fi

  else
    echo "Enter single IP address:"
    read ips
  fi
  break
done

echo -n "Delay between ping sweeps in seconds [1]: "
read input
if [ "$input" == "" ]
then
  input=1
fi
loopdelay=$input

echo -n "Delay between echos in seconds [1]: "
read input
if [ "$intput" == "" ]
then
  input=1
fi
echodelay=$input

echo -n "Echos sent to each IP in each sweep [1]: "
read input
if [ "$input" == "" ]
then
  input=1
fi
if [ $input -lt 1 ]
then
  input=1
fi
echocount=$input

echo -n "Echo loss threshold to each IP each sweep [1]: "
read input
if [ "$input" == "" ]
then
  input=1
fi
if [ $input -lt 1 ]
then
  input=1
fi
echothreshold=$input

echo -n "Display successful echos [n]: "
read input
if [ "$input" == "" ]
then
  input="n"
fi
if [ "$input" != "n" ] && [ "$input" != "y" ]
then
  input="n"
fi
displaysuccess="$input"


loopcount=0
#loopdelay=1
#echocount=1
#echothreshold=1
#echodelay=1
echossent=0
echosfailed=0

startdate=`date`
echo -e "\nStarting at: $startdate"
echo "Loop delay (seconds): $loopdelay"
echo "Echo count per IP: $echocount"
echo "Echo loss threshold per IP: $echothreshold"
echo -e "Echo delay (seconds): $echodelay\n"

trap exitstats 2 3 15

exitstats()
{
 echosreceived=`expr $echossent - $echosfailed`
 receivedpercent=`echo "scale=6; ($echosreceived/$echossent) * 100" | bc`
 elapsedseconds=`expr $loopcount \* $loopdelay`
 echo "$secondselapsed"
 echo -e "\nTimbeeeeer! : `date`"
 echo "Started at: $startdate"
 echo "Loop iterations: $loopcount"
 echo "Run time: `awk -v seconds=$elapsedseconds 'BEGIN {print strftime("%H:%M:%S", seconds, 1)}'`"
 echo "Echo count per IP: $echocount"
 echo "Echo loss threshold per IP: $echothreshold"
 echo "Echo delay (seconds): $echodelay"
 echo "Echos sent: $echossent"
 echo "Replies received: $echosreceived"
 echo "Replies lost: $echosfailed"
 echo "Success rate: $receivedpercent%"
 exit
}


while true
do
  loopcount=$(($loopcount+1))
  for host in ${ips[@]}
  do
    result=$(ping -c $echocount -i $echodelay -s 56 -W 1 $host)
    count=$(echo "$result" | grep -c "64 bytes")
    if [ "$displaysuccess" == "y" ]
    then
      echo "`date` : $(echo "$result" | grep "64 bytes")"
    fi
    echossent=$(($echossent+$count))
    if [ $count -lt $echothreshold ]
    then
      let echosfailed=echosfailed+`expr $echothreshold - $count`
      echo "Failed attempt: $loopcount  Host: $host  Replies: $count  Time: `date +"%Y-%m-%d %H:%M:%S"`"
    fi
  done
  sleep $loopdelay
done 

Previous page: Max MTU Size
Next page: PeeringDB API Examples