[SCRIPT] Show Active Host Information from Network

Post new topic   Reply to topic    DD-WRT Forum Index -> Atheros WiSOC basierende Hardware
Author Message
sebsch1991
DD-WRT Novice


Joined: 11 Nov 2010
Posts: 17

PostPosted: Thu Jul 17, 2014 22:05    Post subject: [SCRIPT] Show Active Host Information from Network Reply with quote
I wrote a script that shows information about Clients in the Network!

Check criterias are
-Ping
-ARP Entries
-/etc/hosts entries
-leases from DHCP

How to install
1. Go to Webinterface -> Administration -> Commands
2. Copy script and paste it into the CommandBox!
3. Click "Save Startup"
You´re done !

Reboot your Router and wait about 5 minutes to let the script collect data! (Every IP needs 2 seconds in worst case)

Now you can visit http://Your_Routers_IP/user/status_log.html

Change the firstIP, lastIP and sleeptime variable in the script to match your private network!

Code:

## status_log.sh
## by sebsch1991
## handam000@yahoo.de
##
## This Script only works if your SubnetMask is 24 or more bit !
##
while true
do

firstIP=2.2.2.1 # Set first usable IP-Address in your Subnet here (EXAMPLE 192.168.0.0/24 -> First usable IP is 192.168.0.1)
lastIP=2.2.2.62 # Set last usable IP-Address in your Subnet here (EXAMPLE 192.168.0.0/24 -> Last usable IP is 192.168.0.254)
sleeptime=300   # Set time to wait til the script will run again and refresh the informations! (at least 2 seconds per IP in your range!)

net="$(echo $lastIP | cut -d "." -f-3)."
count=$(echo $firstIP | cut -d "." -f4)
ACTIVE_OUTPUT="`date`<br><br>Active Clients:<br><br>"
OFFLINE_OUTPUT='Known Offline Clients:<br><br>'
while [[ $count -le $(echo $lastIP| cut -d "." -f4) ]]
do
   ping=0
   arp=0
   hosts=0
   leases=0
   
   ping -c 1 -w 2 ${net}$count &> /dev/null && ping=1
   arp -a | grep -w ${net}$count | grep -v '<incomplete>' &> /dev/null && arp=1
   hosts_hostname=`grep -w ${net}${count} /etc/hosts` ; [ -z $hosts_hostname ] || hosts=1
   if [ $hosts == 0 ]
   then
   leases_hostname=`nvram get dnsmasq_lease_${net}${count} | cut -d " " -f5-`
   [ -z $leases_hostname ] || leases=1
   fi
   
   if [ $ping == 1 ] || [ $arp == 1 ]
   then
   ACTIVE_OUTPUT=${ACTIVE_OUTPUT}$(echo "${net}${count} `[ $ping == 1 ] && echo "PING "``[ $arp == 1 ] && echo "ARP "``[ $hosts == 1 ] && echo "PERMANENT_HOST $hosts_hostname "``[ $leases == 1 ] && echo "LEASES $leases_hostname "`<br>")
   elif [ $hosts == 1 ] || [ $leases == 1 ]
   then
   OFFLINE_OUTPUT=${OFFLINE_OUTPUT}$(echo "${net}${count} `[ $arp == 1 ] && echo "ARP "``[ $hosts == 1 ] && echo "PERMANENT_HOST $hosts_hostname "``[ $leases == 1 ] && echo "LEASES $leases_hostname "`<br>")
   fi
   count=$((${count}+1))
done
echo $ACTIVE_OUTPUT > /tmp/www/status_log.html
echo '<br>' >> /tmp/www/status_log.html
echo $OFFLINE_OUTPUT >> /tmp/www/status_log.html
echo '<br>' >> /tmp/www/status_log.html

echo 'Latest Log Entries:<br><br>' >> /tmp/www/status_log.html
tail -n 1000 /var/log/messages | while read line
do
   echo "$line <br>" >> /tmp/www/status_log.html
done
sleep $sleeptime
done


OUTPUT DESCRIPTION:
2.2.2.15 PING ARP PERMANENT_HOST 2.2.2.15 test-pc
2.2.2.20 PING PERMANENT_HOST 2.2.2.20 testpc2
2.2.2.50 LEASES android-91560b963c657451 01:cc:3a:61:92:12:33


test-pc is reachable by ping and listet in arp table
testpc2 is reachable by ping not listet in arp table
2.2.2.50 is not reachable and was known as a DHCP client




Tested on TP-Link 1043nd v1(r24461) - WORKING!
Sponsor
sebsch1991
DD-WRT Novice


Joined: 11 Nov 2010
Posts: 17

PostPosted: Thu Jul 17, 2014 22:06    Post subject: Reply with quote
Maybe someone with better HTML Skills can help me to improve the output style!

Please report back!
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Atheros WiSOC basierende Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum