Rev 672 | Rev 783 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log
Rev | Author | Line No. | Line |
---|---|---|---|
672 | richard | 1 | #!/bin/bash |
64 | franck | 2 | # $Id: alcasar-watchdog.sh 771 2011-11-16 21:20:42Z richard $ |
672 | richard | 3 | |
4 | # alcasar-watchdog.sh |
||
5 | # by Richard REY |
||
6 | # This script is distributed under the Gnu General Public License (GPL) |
||
7 | |||
308 | richard | 8 | # Ce script prévient les usagers de l'indisponibilité de l'accès Internet |
9 | # il déconnecte les usagers dont |
||
376 | franck | 10 | # - les équipements réseau ne répondent plus |
1 | root | 11 | # - les adresses MAC sont usurpées |
308 | richard | 12 | # This script tells users that Internet access is down |
13 | # it logs out users whose |
||
1 | root | 14 | # - PCs are quiet |
15 | # - MAC address are in used by other systems (usurped) |
||
672 | richard | 16 | |
308 | richard | 17 | EXTIF="eth0" |
1 | root | 18 | INTIF="eth1" |
19 | PRIVATE_IP="192.168.182.1" |
||
20 | tmp_file="/tmp/watchdog.txt" |
||
316 | richard | 21 | DIR_WEB="/var/www/html" |
360 | richard | 22 | Index_Page="$DIR_WEB/index.php" |
597 | richard | 23 | OLDIFS=$IFS |
1 | root | 24 | IFS=$'\n' |
308 | richard | 25 | |
26 | # Fonction appelée si un Pb de connectivité Internet |
||
520 | richard | 27 | # On fait pointer les usagers sur l'adresse locale |
308 | richard | 28 | function ext_down_alert () |
29 | { |
||
30 | case $EXT_DOWN in |
||
31 | "1") |
||
32 | logger "eth0 link down" |
||
363 | richard | 33 | /bin/sed -i "s?diagnostic =.*?diagnostic = \"eth0 link down\";?g" $Index_Page |
308 | richard | 34 | ;; |
35 | "2") |
||
36 | logger "can't contact the default router" |
||
363 | richard | 37 | /bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the default router\";?g" $Index_Page |
308 | richard | 38 | ;; |
39 | "3") |
||
40 | logger "can't contact the Internet DNS" |
||
363 | richard | 41 | /bin/sed -i "s?diagnostic =.*?diagnostic = \"can't contact the Internet DNS\";?g" $Index_Page |
308 | richard | 42 | ;; |
43 | esac |
||
520 | richard | 44 | net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l` |
45 | if [ $net_pb = "0" ] # on alerte les usagers (si ce n'est pas déjà le cas). |
||
308 | richard | 46 | then |
360 | richard | 47 | /bin/sed -i "s?^\$network_pb.*?\$network_pb = True;?g" $Index_Page |
520 | richard | 48 | /bin/sed -i "s?^conf-dir=.*?address=\/#\/$PRIVATE_IP?g" /etc/dnsmasq-blackhole.conf |
49 | /bin/sed -i "1i\address=\/#\/$PRIVATE_IP" /etc/dnsmasq.conf |
||
308 | richard | 50 | /etc/init.d/dnsmasq restart |
51 | fi |
||
52 | } |
||
53 | |||
54 | # On teste la connectivité réseau |
||
55 | # On teste l'état d'EXTIF |
||
56 | EXT_DOWN="0" |
||
57 | if [ "`/usr/sbin/ethtool $EXTIF|grep Link|cut -d' ' -f3`" != "yes" ] |
||
58 | then |
||
59 | EXT_DOWN="1" |
||
60 | fi |
||
61 | # si EXTIF ok, on teste la connectivité vers le routeur par défaut (Box FAI) |
||
62 | if [ $EXT_DOWN -eq "0" ] |
||
63 | then |
||
64 | IP_GW=`/sbin/ip route list|grep ^default|cut -d" " -f3` |
||
65 | arp_reply=`/usr/sbin/arping -I$EXTIF -c1 $IP_GW|grep response|cut -d" " -f2` |
||
66 | if [ $arp_reply -eq "0" ] |
||
67 | then |
||
68 | EXT_DOWN="2" |
||
69 | fi |
||
70 | fi |
||
71 | # si routeur OK, on teste la connectivité vers les DNS externes |
||
72 | # + tard (EXT_DOWN=3) |
||
73 | # si Pb réseau, on avertit les usagers |
||
74 | if [ $EXT_DOWN != "0" ] |
||
75 | then |
||
76 | ext_down_alert |
||
77 | else |
||
520 | richard | 78 | # sinon, le cas échéant, on rebascule en mode normal |
79 | net_pb=`cat /etc/dnsmasq.conf|grep "address=/#/"|wc -l` |
||
80 | if [ $net_pb != "0" ] |
||
308 | richard | 81 | then |
360 | richard | 82 | /bin/sed -i "s?^\$network_pb.*?\$network_pb = False;?g" $Index_Page |
520 | richard | 83 | /bin/sed -i "s?^address=\/#\/.*?conf-dir=/usr/local/etc/alcasar-dnsfilter-enabled?g" /etc/dnsmasq-blackhole.conf |
84 | /bin/sed -i "/^address=/d" /etc/dnsmasq.conf |
||
308 | richard | 85 | /etc/init.d/dnsmasq restart |
86 | fi |
||
87 | fi |
||
1 | root | 88 | # lecture du fichier contenant les adresses IP des stations muettes |
89 | if [ -e $tmp_file ]; then |
||
90 | cat $tmp_file | while read noresponse |
||
91 | do |
||
92 | noresponse_ip=`echo $noresponse | cut -d" " -f1` |
||
93 | noresponse_mac=`echo $noresponse | cut -d" " -f2` |
||
109 | richard | 94 | arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c1 -w4 $noresponse_ip|grep response|cut -d" " -f2` |
1 | root | 95 | if [[ $(expr $arp_reply) -eq 0 ]] |
96 | then |
||
771 | richard | 97 | logger "alcasar-watchdog $noresponse_ip ($noresponse_mac) can't be contact. Alcasar disconnects the user." |
1 | root | 98 | /usr/sbin/chilli_query logout $noresponse_mac |
99 | fi |
||
100 | done |
||
101 | rm $tmp_file |
||
102 | fi |
||
103 | # on traite chaque équipements connus de chilli |
||
520 | richard | 104 | for system in `/usr/sbin/chilli_query list |grep -v "\.0\.0\.0"` |
1 | root | 105 | do |
106 | active_ip=`echo $system |cut -d" " -f2` |
||
107 | active_session=`echo $system |cut -d" " -f5` |
||
108 | active_mac=`echo $system | cut -d" " -f1` |
||
109 | richard | 109 | # on ne traite que les équipements exploitées par un usager authentifié (test de 2 réponses en 4 secondes) |
1 | root | 110 | if [[ $(expr $active_session) -eq 1 ]] |
111 | then |
||
771 | richard | 112 | arp_reply=`/usr/sbin/arping -b -I$INTIF -s$PRIVATE_IP -c2 -w4 $active_ip|grep "Unicast reply"|wc -l` |
1 | root | 113 | # on stocke les adresses IP des stations muettes |
114 | if [[ $(expr $arp_reply) -eq 0 ]] |
||
115 | then |
||
116 | echo "$active_ip $active_mac" >> $tmp_file |
||
117 | fi |
||
118 | # on deconnecte l'usager d'une stations usurpée (@MAC) |
||
119 | if [[ $(expr $arp_reply) -gt 2 ]] |
||
120 | then |
||
771 | richard | 121 | logger "alcasar-watchdog : $active_ip is usurped ($active_mac). Alcasar disconnect the user." |
1 | root | 122 | /usr/sbin/chilli_query logout $active_mac |
123 | fi |
||
124 | fi |
||
125 | done |
||
597 | richard | 126 | IFS=$OLDIFS |