Find ip devices on network

This simple script pings a range of addresses to find active devices.

#!/bin/bash

#intended to loop through range of addresses and ping them to discover active devices.
#This fails of course if a device is set to ignore icmp.

#turn on debugging, -x starts, +x stops
set +x


#BASEIP=10.106.18
BASEIP=192.168.1


DNSSERVER=192.168.1.1

for IPADDRESS in {1..50}
do
        #echo "Testing >${BASEIP}.${IPADDRESS}<."

        #Prime Ping with a dummy ping which we ignore, timeout 2 seconds
        ping -t 2 -c1 ${BASEIP}.${IPADDRESS}  2>&1 > /dev/null

        #Now record the result, RC0=reply, RC1=no reply, RC2=sent but no reply (mac) 
        ping -t 2 -c1 ${BASEIP}.${IPADDRESS}  2>&1 > /dev/null
        PINGRESULT=$?

        if [ "${PINGRESULT}" -eq 0 ]; then
                echo " "
                echo "Device found on ${BASEIP}.${IPADDRESS}"
                DNSNAME=`dig @${DNSSERVER} -x ${BASEIP}.${IPADDRESS} | grep -A1  ";; ANSWER" | grep -v ";;"` 2>&1 > /dev/null
		LOOKUPRET=$?

		if [ "${LOOKUPRET}" -eq 0 ]; then
			echo "DNS lookup gives >${DNSNAME}<."
		else 
			echo "No dns lookup found"
		fi

        elif [ "${PINGRESULT}" -eq 1 ]; then
                DUMMY=1
                echo -n "."
        elif [ "${PINGRESULT}" -eq 2 ]; then
		#Mac rc=2 means ping sent ok, but no reply
                DUMMY=1
                echo -n "."

        else
                echo "Unknown return code, >${PINGRESULT}<"       

fi

done
echo " "


exit 0

rb/findipdevices.txt · Last modified: 10/10/2013 15:21 (external edit)