Subversion Repositories ALCASAR

Rev

Rev 672 | Rev 783 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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