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