Rev 308 | Rev 360 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log
#!/bin/sh
# $Id: alcasar-watchdog.sh 309 2010-10-26 21:58:25Z richard $
# by rexy
# Ce script prévient les usagers de l'indisponibilité de l'accès Internet
# il déconnecte les usagers dont
# - les équipementis réseau ne répondent plus
# - les adresses MAC sont usurpées
# This script tells users that Internet access is down
# it logs out users whose
# - PCs are quiet
# - MAC address are in used by other systems (usurped)
EXTIF="eth0"
INTIF="eth1"
PRIVATE_IP="192.168.182.1"
tmp_file="/tmp/watchdog.txt"
Network_Pb_Page="/var/www/html/redirect/index-network-pb.php"
IFS=$'\n'
# Fonction appelée si un Pb de connectivité Internet
# On fait pointer les usagers sur une page d'erreur
function ext_down_alert ()
{
case $EXT_DOWN in
"1")
logger "eth0 link down"
/bin/sed -i "s?diagnostic =.*?diagnostic = eth0 link down?g" $Network_Pb_Page
;;
"2")
logger "can't contact the default router"
/bin/sed -i "s?diagnostic =.*?diagnostic = can't contact the default router?g" $Network_Pb_Page
;;
"3")
logger "can't contact the Internet DNS"
/bin/sed -i "s?diagnostic =.*?diagnostic = can't contact the Internet DNS?g" $Network_Pb_Page
;;
esac
net_pb=`cat /etc/dnsmasq.d/alcasar-dnsmasq.conf|grep "address=/#/"|wc -l`
if [ $net_pb != "1" ]
then
ln -sf /var/www/html/redirect/index-network-pb.php /var/www/html/redirect/index.php
/bin/sed -i "s?^conf-dir=.*?address=\/#\/$PRIVATE_IP?g" /etc/dnsmasq.d/alcasar-dnsmasq.conf
/etc/init.d/dnsmasq restart
fi
}
# On teste la connectivité réseau
# On teste l'état d'EXTIF
EXT_DOWN="0"
if [ "`/usr/sbin/ethtool $EXTIF|grep Link|cut -d' ' -f3`" != "yes" ]
then
EXT_DOWN="1"
fi
# si EXTIF ok, on teste la connectivité vers le routeur par défaut (Box FAI)
if [ $EXT_DOWN -eq "0" ]
then
IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3`
arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2`
if [ $arp_reply -eq "0" ]
then
EXT_DOWN="2"
fi
fi
# si routeur OK, on teste la connectivité vers les DNS externes
# + tard (EXT_DOWN=3)
# si Pb réseau, on avertit les usagers
if [ $EXT_DOWN != "0" ]
then
ext_down_alert
else
# sinon, on rebascule en mode normal
net_pb=`cat /etc/dnsmasq.d/alcasar-dnsmasq.conf|grep "address=/#/"|wc -l`
if [ $net_pb -eq "1" ]
then
ln -sf /var/www/html/redirect/index-access-deny.php /var/www/html/redirect/index.php
/bin/sed -i "s?^address=\/#\/.*?conf-dir=/usr/local/etc/alcasar-dnsfilter-enabled?g" /etc/dnsmasq.d/alcasar-dnsmasq.conf
/etc/init.d/dnsmasq restart
fi
fi
# lecture du fichier contenant les adresses IP des stations muettes
if [ -e $tmp_file ]; then
cat $tmp_file | while read noresponse
do
noresponse_ip=`echo $noresponse | cut -d" " -f1`
noresponse_mac=`echo $noresponse | cut -d" " -f2`
arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep response|cut -d" " -f2`
if [[ $(expr $arp_reply) -eq 0 ]]
then
logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) reste muette. On déconnecte."
/usr/sbin/chilli_query logout $noresponse_mac
fi
done
rm $tmp_file
fi
# on traite chaque équipements connus de chilli
for system in `/usr/sbin/chilli_query list`
do
active_ip=`echo $system |cut -d" " -f2`
active_session=`echo $system |cut -d" " -f5`
active_mac=`echo $system | cut -d" " -f1`
# on ne traite que les équipements exploitées par un usager authentifié (test de 2 réponses en 4 secondes)
if [[ $(expr $active_session) -eq 1 ]]
then
arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep response|cut -d" " -f2`
# on stocke les adresses IP des stations muettes
if [[ $(expr $arp_reply) -eq 0 ]]
then
echo "$active_ip $active_mac" >> $tmp_file
fi
# on deconnecte l'usager d'une stations usurpée (@MAC)
if [[ $(expr $arp_reply) -gt 2 ]]
then
logger "alcasar-watchdog : $active_ip est usurpée ($active_mac). On déconnecte."
/usr/sbin/chilli_query logout $active_mac
fi
fi
done