#! /bin/sh #------------------- Super Ping : sping --------------------------- # File: sping # Purpose: ping a full sub-net (netmask 255.255.255.0) # Date: 07.09.2003 # Author: Michel Bisson # Syntax: sping 192.168.70. (the last number is omitted!) #------------------------------------------------------------------ # Declare error function using the Variable $errortext: # Displays error message and exits with error code 1 error () { echo "$errortext" echo "Syntax: eg. $0 192.168.70. " exit 1 } # --------Check the validity of the given parameter (Partial IP) # Only one parameter if [ "$#" -ne 1 ] ; then errortext="ERROR: Incorrect number of parameters." error fi # ----------Parameter is a correct Partial IP? Correct it if possible # Accept if xxx.yyy.zzz. or xxx.yyy.zzz if ! (echo $1 | egrep "[0-9]*\.[0-9]*\.[0-9]*\.?$" &>/dev/null) ; then errortext="ERROR: Bad subnet Partial IP Syntax" error fi # Verify validity if all numbers in IP (0-255) IFS="." len=0 for num in $1 ; do let "len+=1" if [ "$num" -gt 255 ] ; then errortext="ERROR: Wrong values in partial IP Syntax." error fi if [ "$num" = "" -a "$len" -le 3 ] ; then errortext="ERROR: Wrong format of partial network IP." error fi done IFS=" " # Is IP long enough? if [ "$len" -le 2 ] ; then errortext="ERROR: Wrong number of items in partial network IP." error fi # Check if subnet IP is valid for local machine in network if ! (ifconfig | grep "inet " | grep $1 &>/dev/null) ; then errortext="ERROR: Partial Subnet IP is not in our local subnet" error fi # Correct by adding a '.' if missing at the end if (echo $1 | egrep "\.$" &>/dev/null) ; then netip="$1" else netip="$1." fi #echo $netip #-------- Generate all the IPs from xx.xx.xx.1 to xx.xx.xx.254-- netnum=1 #------- Create or Empty the temporary file --------- echo > /tmp/sping #------- ping them all almost at the same time (sent as separate jobs) until [ $netnum -eq 255 ]; do /bin/ping -w3 -c1 $netip$netnum 1>>/tmp/sping & let "netnum+=1" done #----------- wait a bit to let some answers back ---------- sleep 4 #----------- Kill all the ping that are still waiting --- killall ping &> /dev/null #----------- Show only the pings that received an answer -- grep '64 bytes from' /tmp/sping | cut -d: -f1 | cut -d" " -f4 | \ sort -t. -k4n | tee /tmp/nethosts exit 0