IP management

Post date: Jul 25, 2011 12:28:52 AM

Friday, April 18, 2008, 03:52 PM

Posted by Administrator

You need to know what is on your network, and you need to keep your information current. How? Well, why not ping everything in range and then do arp and dns lookups for the active ones? Here is the script I used:

sysresccd IPMgt # cat ipfinder.command

function main

{

quittingtime=`date +%s -d+5days+14hours`

while [ `date +%s` -lt $quittingtime ]

do

if [ -f NonLiveIPs.current.txt ]

then

mv NonLiveIPs.current.txt NonLiveIPs.old.txt

fi

touch NonLiveIPs.current.txt

touch LiveIPs.txt

firstpart='192.168.0.'

lastpart=1

while [ $lastpart -lt 255 ]

do

(

# # %03s - three characters, padded with zeros

strlastpart=$(echo $lastpart|(awk '{printf "%03s", $1}'))

thiscommand="ping -c3 -q ${firstpart}${lastpart} 2>/dev/null|grep '100% packet loss'"

cmdresult=$(eval "$thiscommand")

echo "DEBUG: thiscommand: $thiscommand"

echo "DEBUG: cmdresult: $cmdresult"|cut -b1-80

if [ -n "$cmdresult" ]

then

echo "DEBUG: noresponse processing"

thiscommand="grep ${firstpart}{strlastpart} LiveIPs.txt"

cmdresult=$(eval "$thiscommand")

echo "DEBUG: cmdresult: $cmdresult"|cut -b1-80

if [ -z "$cmdresult" ]

then

echo "DEBUG: noresponseX2 processing"

echo "No Response: ${firstpart}${lastpart}" | tee -a NonLiveIPs.current.txt

else

echo "No Response: ${firstpart}${lastpart} - Temporarily down?"

fi

else

echo "DEBUG: responded, processing"

arpresult="$(arping2 -c1 ${firstpart}${lastpart}|grep 'index')"

echo "DEBUG: arpresult: $arpresult"

macaddr="$(echo $arpresult|awk '{print $4}')"

echo "DEBUG: macaddr: $macaddr"

if [ -z "$macaddr" ];then macaddr="00:00:00:00:00:00";fi

datestamp=$(date +%s.%d%b%Y_%H.%M.%S)

echo "DEBUG: datestamp: $datestamp"

dnsname="$(nslookup ${firstpart}${lastpart}|grep 'name'|awk -F= '{print $2}')"

echo "DEBUG: dnsname: $dnsname"

if [ -z "$dnsname" ];then dnsname=" unknown.dtfcu.com";fi

echo "${firstpart}${strlastpart} $macaddr $datestamp $dnsname" |tee -a LiveIPs.tmp

fi

) &

let lastpart++

if [ $(expr $lastpart % 5) -eq 0 ];then sleep 1;fi

done

echo "DEBUG: pausing for a minute to ensure all processing finished"

sleep 60 #Make sure the delay stays here so everything finishes first

cleanlist

echo "Pausing for five minutes"

sleep 300

date

done

}

function cleanlist

{

sort LiveIPs.tmp|uniq >LiveIPs.txt #Must follow delay

rm -f LiveIPs.tmp;touch LiveIPs.tmp

for i in `awk '{print $1"_"$2}'<LiveIPs.txt|sort|uniq`

do

echo "i: $i"

searchstr=$(echo "$i"|sed 's/_/ /g')

LastUniqContact=$(grep -F "$searchstr" LiveIPs.txt|tail -n1)

echo "$LastUniqContact"|tee -a LiveIPs.tmp

done

mv -f LiveIPs.tmp LiveIPs.txt

}

cleanlist

echo "List cleaned";sleep 1;date;

main