838 |
richard |
1 |
#/bin/bash
|
|
|
2 |
# $Id: alcasar-nf.sh 791 2012-01-13 21:31:36Z richard $
|
|
|
3 |
|
|
|
4 |
# alcasar-dhcp.sh
|
|
|
5 |
# by Rexy
|
|
|
6 |
# This script is distributed under the Gnu General Public License (GPL)
|
|
|
7 |
|
|
|
8 |
# active ou desactive le service DHCP sur le réseau de consultation
|
|
|
9 |
# enable or disable the DHCP service on consultation LAN
|
|
|
10 |
|
|
|
11 |
SED="/bin/sed -i"
|
841 |
richard |
12 |
CHILLI_CONF_FILE="/etc/chilli.conf"
|
838 |
richard |
13 |
ALCASAR_CONF_FILE="/usr/local/etc/alcasar.conf"
|
861 |
richard |
14 |
DNSMASQ_CONF_FILE="/etc/dnsmasq.conf"
|
841 |
richard |
15 |
ETHERS_FILE="/usr/local/etc/alcasar-ethers"
|
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
|
861 |
richard |
32 |
tmp_mask=`echo $PRIVATE_NETWORK_MASK|cut -d"/" -f2`; half_mask=`expr $tmp_mask + 1` # masque du 1/2 réseau de consultation (ex.: 25)
|
|
|
33 |
PRIVATE_STAT_IP=$PRIVATE_NETWORK/$half_mask # plage des adresses statiques (ex.: 192.168.182.0/25)
|
|
|
34 |
private_network_ending=`echo $PRIVATE_NETWORK | cut -d"." -f$classe_sup` # dernier octet de l'@ de réseau
|
|
|
35 |
private_broadcast_ending=`echo $PRIVATE_BROADCAST | cut -d"." -f$classe_sup` # dernier octet de l'@ de broadcast
|
|
|
36 |
private_plage=`expr $private_broadcast_ending - $private_network_ending + 1`
|
|
|
37 |
private_half_plage=`expr $private_plage / 2`
|
|
|
38 |
private_dyn=`expr $private_half_plage + $private_network_ending`
|
|
|
39 |
private_dyn_ip_network=`echo $PRIVATE_NETWORK | cut -d"." -f1-$classe`"."$private_dyn"."`echo $PRIVATE_NETWORK | cut -d"." -f$classe_sup_sup-5`
|
|
|
40 |
PRIVATE_DYN_IP=`echo $private_dyn_ip_network | cut -d"." -f1-4`/$half_mask # @ réseau (CIDR) de la plage des adresses dynamiques (ex.: 192.168.182.128/25)
|
|
|
41 |
private_dyn_ip_ending=`echo $private_dyn_ip_network | cut -d"." -f4`
|
|
|
42 |
PRIVATE_DYN_FIRST_IP=`echo $private_dyn_ip_network | cut -d"." -f1-3`"."`expr $private_dyn_ip_ending + 1` # 1ère adresse de la plage dynamique (ex.: 192.168.182.129)
|
|
|
43 |
PRIVATE_DYN_LAST_IP=`echo $PRIVATE_BROADCAST | cut -d"." -f1-3`"."`expr $private_broadcast_ending - 1` # dernière adresse de la plage dynamique (ex.: 192.168.182.254)
|
841 |
richard |
44 |
|
861 |
richard |
45 |
|
|
|
46 |
usage="Usage: alcasar-dhcp.sh {--full | -full} | {--off | -off} | {--half | -half}"
|
838 |
richard |
47 |
nb_args=$#
|
|
|
48 |
args=$1
|
|
|
49 |
if [ $nb_args -eq 0 ]
|
|
|
50 |
then
|
|
|
51 |
echo "$usage"
|
|
|
52 |
exit 1
|
|
|
53 |
fi
|
|
|
54 |
case $args in
|
861 |
richard |
55 |
-\? | -h | --h)
|
838 |
richard |
56 |
echo "$usage"
|
|
|
57 |
exit 0
|
|
|
58 |
;;
|
|
|
59 |
--off|-off) # disable DHCP service
|
861 |
richard |
60 |
$SED "s?.*statip.*?statip\t\t$PRIVATE_NETWORK_MASK?g" $CHILLI_CONF_FILE
|
841 |
richard |
61 |
$SED "s?^#nodynip.*?nodynip?g" $CHILLI_CONF_FILE
|
861 |
richard |
62 |
$SED "s?^dynip.*?#dynip?g" $CHILLI_CONF_FILE
|
|
|
63 |
$SED "s?^#dynip.*?#dynip?g" $CHILLI_CONF_FILE
|
|
|
64 |
# $SED "s?^ethers.*?#ethers\t\t$ETHERS_FILE?g" $CHILLI_CONF_FILE
|
838 |
richard |
65 |
$SED "s?^DHCP.*?DHCP=off?g" $ALCASAR_CONF_FILE
|
|
|
66 |
/etc/init.d/chilli restart
|
|
|
67 |
;;
|
861 |
richard |
68 |
--full|-full) # enable DHCP service on all range of IP addresses
|
|
|
69 |
$SED "s?^.*statip.*?#statip?g" $CHILLI_CONF_FILE
|
841 |
richard |
70 |
$SED "s?^nodynip.*?#nodynip?g" $CHILLI_CONF_FILE
|
861 |
richard |
71 |
# $SED "s?^#ethers.*?ethers\t\t$ETHERS_FILE?g" $CHILLI_CONF_FILE
|
|
|
72 |
$SED "s?^DHCP.*?DHCP=full?g" $ALCASAR_CONF_FILE
|
|
|
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
|
841 |
richard |
76 |
/etc/init.d/chilli restart
|
|
|
77 |
;;
|
861 |
richard |
78 |
--half|-half) # enable DHCP service on half (upper) range of IP addresses
|
|
|
79 |
$SED "s?.*statip.*?statip\t\t$PRIVATE_STAT_IP?g" $CHILLI_CONF_FILE
|
|
|
80 |
$SED "s?^nodynip.*?#nodynip?g" $CHILLI_CONF_FILE
|
|
|
81 |
# $SED "s?^#ethers.*?ethers\t\t$ETHERS_FILE?g" $CHILLI_CONF_FILE
|
|
|
82 |
$SED "s?^DHCP.*?DHCP=half?g" $ALCASAR_CONF_FILE
|
|
|
83 |
$SED "s?^dynip.*?dynip\t\t$PRIVATE_DYN_IP?g" $CHILLI_CONF_FILE
|
|
|
84 |
$SED "s?^#dynip.*?dynip\t\t$PRIVATE_DYN_IP?g" $CHILLI_CONF_FILE
|
|
|
85 |
$SED "s?^dhcp_range.*?dhcp-range=$PRIVATE_DYN_FIRST_IP,$PRIVATE_DYN_LAST_IP,$PRIVATE_NETMASK,12h?g" $DNSMASQ_CONF_FILE
|
|
|
86 |
|
|
|
87 |
/etc/init.d/chilli restart
|
|
|
88 |
;;
|
838 |
richard |
89 |
*)
|
|
|
90 |
echo "Argument inconnu :$1";
|
|
|
91 |
echo "$usage"
|
|
|
92 |
exit 1
|
|
|
93 |
;;
|
|
|
94 |
esac
|
|
|
95 |
|