Subversion Repositories ALCASAR

Rev

Rev 2013 | Rev 2454 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2223 tom.houday 1
#!/bin/bash
958 franck 2
# $Id: alcasar-dhcp.sh 2223 2017-05-14 14:38:01Z tom.houdayer $
838 richard 3
 
4
# alcasar-dhcp.sh
1783 franck 5
 
838 richard 6
# by Rexy
7
# This script is distributed under the Gnu General Public License (GPL)
8
 
9
# active ou desactive le service DHCP sur le réseau de consultation
10
# enable or disable the DHCP service on consultation LAN
11
 
12
SED="/bin/sed -i"
841 richard 13
CHILLI_CONF_FILE="/etc/chilli.conf"
838 richard 14
ALCASAR_CONF_FILE="/usr/local/etc/alcasar.conf"
861 richard 15
DNSMASQ_CONF_FILE="/etc/dnsmasq.conf"
838 richard 16
 
861 richard 17
# define DHCP parameters (LAN side)
841 richard 18
PRIVATE_IP_MASK=`grep PRIVATE_IP $ALCASAR_CONF_FILE|cut -d"=" -f2`
19
PRIVATE_IP=`echo $PRIVATE_IP_MASK | cut -d"/" -f1`
861 richard 20
PRIVATE_PREFIX=`/bin/ipcalc -p $PRIVATE_IP_MASK |cut -d"=" -f2`				# network prefix (ie. 24)
841 richard 21
PRIVATE_NETMASK=`/bin/ipcalc -m $PRIVATE_IP_MASK | cut -d"=" -f2`
861 richard 22
PRIVATE_NETWORK=`/bin/ipcalc -n $PRIVATE_IP $PRIVATE_NETMASK| cut -d"=" -f2`
841 richard 23
PRIVATE_PREFIX=`/bin/ipcalc -p $PRIVATE_IP $PRIVATE_NETMASK |cut -d"=" -f2`
861 richard 24
PRIVATE_NETWORK_MASK=$PRIVATE_NETWORK/$PRIVATE_PREFIX					# ie.: 192.168.182.0/24
25
classe=$((PRIVATE_PREFIX/8)); classe_sup=`expr $classe + 1`; classe_sup_sup=`expr $classe + 2`		# ie.: 2=classe B, 3=classe C
26
PRIVATE_BROADCAST=`/bin/ipcalc -b $PRIVATE_NETWORK_MASK | cut -d"=" -f2`		# private network broadcast (ie.: 192.168.182.255)
27
private_network_ending=`echo $PRIVATE_NETWORK | cut -d"." -f$classe_sup`		# last octet of LAN address
28
private_broadcast_ending=`echo $PRIVATE_BROADCAST | cut -d"." -f$classe_sup`		# last octet of LAN broadcast
29
PRIVATE_FIRST_IP=`echo $PRIVATE_NETWORK | cut -d"." -f1-3`"."`expr $private_network_ending + 1`		# First network address (ex.: 192.168.182.1)
30
PRIVATE_LAST_IP=`echo $PRIVATE_BROADCAST | cut -d"." -f1-3`"."`expr $private_broadcast_ending - 1`	# last network address (ex.: 192.168.182.254)
841 richard 31
PRIVATE_NETWORK_MASK=$PRIVATE_NETWORK/$PRIVATE_PREFIX
967 franck 32
EXT_DHCP_IP=`grep EXT_DHCP_IP $ALCASAR_CONF_FILE|cut -d"=" -f2`				# Adresse du serveur DHCP externe
919 franck 33
RELAY_DHCP_IP=`grep RELAY_DHCP_IP $ALCASAR_CONF_FILE|cut -d"=" -f2`			# Adresse de l'agent Relay : IP interne (défaut 192.168.182.1) dans le cas de DHCP dans le LAN de consultation
1469 richard 34
RELAY_DHCP_IP=${RELAY_DHCP_IP:=$PRIVATE_IP}						# IP externe (défaut x.y.z.t) dans le cas de DHCP du côté WAN
916 franck 35
RELAY_DHCP_PORT=`grep RELAY_DHCP_PORT $ALCASAR_CONF_FILE|cut -d"=" -f2`			# Port de redirection vers le relay DHCP :  67 par défaut
36
RELAY_DHCP_PORT=${RELAY_DHCP_PORT:=67}
841 richard 37
 
2013 raphael.pi 38
usage="Usage: alcasar-dhcp.sh {--on | -on} | {--off | -off} "
838 richard 39
nb_args=$#
40
args=$1
41
if [ $nb_args -eq 0 ]
42
then
43
	echo "$usage"
44
	exit 1
45
fi
46
case $args in
861 richard 47
	-\? | -h | --h)
838 richard 48
		echo "$usage"
49
		exit 0
50
		;;
51
	--off|-off) # disable DHCP service
861 richard 52
		$SED "s?.*statip.*?statip\t\t$PRIVATE_NETWORK_MASK?g" $CHILLI_CONF_FILE
841 richard 53
		$SED "s?^#nodynip.*?nodynip?g" $CHILLI_CONF_FILE
861 richard 54
		$SED "s?^dynip.*?#dynip?g" $CHILLI_CONF_FILE
55
		$SED "s?^#dynip.*?#dynip?g" $CHILLI_CONF_FILE
838 richard 56
		$SED "s?^DHCP.*?DHCP=off?g" $ALCASAR_CONF_FILE
1040 franck 57
		if [ "$EXT_DHCP_IP" != "none" ] 
914 franck 58
		then
1157 stephane 59
		      $SED "s?.*dhcpgateway\t.*?dhcpgateway\t\t $EXT_DHCP_IP?g" $CHILLI_CONF_FILE
914 franck 60
		      $SED "s?.*dhcprelayagent.*?dhcprelayagent\t\t$RELAY_DHCP_IP?g" $CHILLI_CONF_FILE
1101 franck 61
		      $SED "s?.*dhcpgatewayport.*?dhcpgatewayport\t\t$RELAY_DHCP_PORT?g" $CHILLI_CONF_FILE
62
		else
1780 c.valfort 63
		      $SED "s?.*dhcpgateway\t.*?#dhcpgateway\t\t$EXT_DHCP_IP?g" $CHILLI_CONF_FILE
1101 franck 64
		      $SED "s?.*dhcprelayagent.*?#dhcprelayagent\t\t$RELAY_DHCP_IP?g" $CHILLI_CONF_FILE
914 franck 65
		      $SED "s?.*dhcpgatewayport.*?#dhcpgatewayport\t\t$RELAY_DHCP_PORT?g" $CHILLI_CONF_FILE
66
		fi
1419 richard 67
		/usr/bin/systemctl restart chilli
838 richard 68
		;;
1484 richard 69
	--on|-on) # enable DHCP service on all range of IP addresses
861 richard 70
		$SED "s?^.*statip.*?#statip?g" $CHILLI_CONF_FILE
841 richard 71
		$SED "s?^nodynip.*?#nodynip?g" $CHILLI_CONF_FILE
1484 richard 72
		$SED "s?^DHCP.*?DHCP=on?g" $ALCASAR_CONF_FILE
861 richard 73
		$SED "s?^dynip.*?dynip\t\t$PRIVATE_NETWORK_MASK?g" $CHILLI_CONF_FILE
74
		$SED "s?^#dynip.*?dynip\t\t$PRIVATE_NETWORK_MASK?g" $CHILLI_CONF_FILE
75
		$SED "s?^dhcp_range.*?dhcp-range=$PRIVATE_FIRST_IP,$PRIVATE_LAST_IP,$PRIVATE_NETMASK,12h?g" $DNSMASQ_CONF_FILE
1157 stephane 76
		$SED "s?^dhcpgateway\t.*?#dhcpgateway\t\t $EXT_DHCP_IP?g" $CHILLI_CONF_FILE
914 franck 77
		$SED "s?^dhcprelayagent.*?#dhcprelayagent\t\t$RELAY_DHCP_IP?g" $CHILLI_CONF_FILE
78
		$SED "s?^dhcpgatewayport.*?#dhcpgatewayport\t\t$RELAY_DHCP_PORT?g" $CHILLI_CONF_FILE
1780 c.valfort 79
		$SED "s?^EXT_DHCP_IP.*?EXT_DHCP_IP=$EXT_DHCP_IP?g" $ALCASAR_CONF_FILE
80
		$SED "s?^RELAY_DHCP_IP.*?RELAY_DHCP_IP=$RELAY_DHCP_IP?g" $ALCASAR_CONF_FILE
81
		$SED "s?^RELAY_DHCP_PORT.*?RELAY_DHCP_PORT=$RELAY_DHCP_PORT?g" $ALCASAR_CONF_FILE
1419 richard 82
		/usr/bin/systemctl restart chilli
841 richard 83
		;;
838 richard 84
	*)
85
		echo "Argument inconnu :$1";
86
		echo "$usage"
87
		exit 1
88
		;;
89
esac
90