#!/bin/bash

# Who is Up (whoisup.sh) v0.002c (beta test release)

# Copyright (C) 2000 Ralf Heiringhoff <ralf@freeze.rhwd.de>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA 02111-1307 USA

#
# This Script was written to check a range of Hosts whether they are up
# and what MAC Address they use.
#

#
# Changelog:
#
#       Just started.
#
# TODO:
#
#       Make any other subnetmask than 255.255.255.x work
#	Translate for example:
#			whoisup.sh 172.168.0.0 24 
#					       ^^
#		to
#			whoisup.sh 192.168.0.0 255.255.255.0
#	Write an example Cron entry which is working.
#	Write a scripts to do HTML output.
#	
# BUGS:
#
#   Report  bugs  to  ralf@freeze.rhwd.de. Include a complete,
#   self-contained example that  will  allow  the  bug  to  be
#   reproduced,  and say which version of whoisup.sh you are using.

# Sample Crontab entry
#
# m h dom mon dow user  command
#
# 0 * *	  *   *   root  /usr/local/sbin/whoisup.sh $network $subnetmask

upfile=/tmp/host-up
downfile=/tmp/host-down

##for i in `seq 1 4`
##		do\
##		net$i=`echo $1 | tr '.' ' ' | awk '{ print $$i };'`
##		done

if [ -z "$1" ]; then
	echo -e "\nUse:\nwhoisup.sh [network addr] [subnetmask addr|subnetmask bits] ...\n"
	exit 0
fi
network=${1:-0}
subnetmask=${2:-0}

net1=`echo $network | tr '.' ' ' | awk '{ print $1 };'`
net2=`echo $network | tr '.' ' ' | awk '{ print $2 };'`
net3=`echo $network | tr '.' ' ' | awk '{ print $3 };'`
net4=`echo $network | tr '.' ' ' | awk '{ print $4 };'`


sub1=`echo $subnetmask | tr '.' ' ' | awk '{ print $1 };'`

if [ "$sub1" -ne "255" -a "$sub1" -ge "8" -a "$sub1" -le "32" ]; then

	echo -e "\nNot implemented yet use 255.x.x.x right now ...\n"
	exit 0
	
elif [ "$sub1" -eq "255" ]; then
	sub2=`echo $subnetmask | tr '.' ' ' | awk '{ print $2 };'`
	sub3=`echo $subnetmask | tr '.' ' ' | awk '{ print $3 };'`
	sub4=`echo $subnetmask | tr '.' ' ' | awk '{ print $4 };'`

else
	echo -e "\nMOEP! An error occoured while processing\nthe subnetmask you've Entered.\nPlease enter something like 255.x.x.x or 8 ...\n"

fi

class_a="[ $net1 -ge 1   -a $net1 -le 126 ]"
class_b="[ $net1 -ge 128 -a $net1 -le 191 ]"
class_c="[ $net1 -ge 192 -a $net1 -le 223 ]"

localhost="[ $net1 -eq 127 ]"

class_b_sub="[ $sub2 -eq 255 ]" 
class_c_sub="[ $sub2 -eq 255 -a $sub3 -eq 255 ]" 

sub2_ok="[ ! -z `echo -e "0\n128\n192\n224\n240\n248\n252\n255" | grep -x $sub2` ]"
sub3_ok="[ ! -z `echo -e "0\n128\n192\n224\n240\n248\n252\n255" | grep -x $sub3` ]"
sub4_ok="[ ! -z `echo -e "0\n128\n192\n224\n240\n248\n252\n255" | grep -x $sub4` ]"


if	$class_a; then

	echo -e "\nClass A\n"
		
		if [ $sub2_ok -a $sub3_ok -a $sub4_ok ]; then
		
			echo -e "\nNot Yet Supported ...\n"
			exit 0
		
		else
		
			echo -e "\nYou've entered a invalid Subnetmask ...\n"
			exit 0
		
		fi
	
elif	$localhost; then
	
	echo -e "\nPlease don't use 127.x.x.x\n"
	exit 0
	
elif    $class_b; then

	if $class_b_sub; then
		
		echo -e "\nClass B\n"
		
		if [ $sub3_ok -a $sub4_ok ]; then
		
			echo -e "\nNot Yet Supported ...\n"
			exit 0
		
		else
		
			echo -e "\nYou've entered a invalid Subnetmask ...\n"
			exit 0
		
		fi
	else
		
		echo -e "\nYour Subnetmask doesn't match your Network adress ...\n"
		exit 0
	
	fi
	
elif    $class_c; then
	
	if $class_c_sub; then
	
		echo -e "\nClass C\n"
		
		if [ $sub4_ok ]; then
			echo ok
		else
			echo -e "\nYou've entered a invalid Subnetmask ...\n"
			exit 0
		fi
	else
		echo -e "\nYour Subnetmask doesn't match your Network adress ...\n"
		exit 0
	fi
		
fi

size2=$((256-sub2))
size3=$((256-sub3))
size4=$((256-sub4))


netsize=$((size2*size3*size4))
echo "$netsize (256 - $sub4)"

first_ip=$((net4+1))
echo "$first_ip ($net4 + 1)"

last_ip=$((net4+netsize-1))
echo "$last_ip ($net4 + $netsize - 1)"

#exit 0

for host in `seq $first_ip $last_ip`
		do\
		
		cur_host="$net1.$net2.$net3.$host"
		
		if ping -n -c 1 $cur_host ; then
			mac=`/usr/sbin/arp -n | grep $cur_host | awk '{ print $3 }'`
			echo "$cur_host ${mac:=NOT_IN_ARP_CACHE} `date \+\%H:\%M`" >>$upfile
		else
			echo "$cur_host `date \+\%H:\%M`" >>$downfile
		fi
done
