-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadvanced_dhcp.sh
446 lines (421 loc) · 14.1 KB
/
advanced_dhcp.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/bash
intro()
{
msgbox "Here you can reconfigure your DHCP server or add more advanced options such as static IP-Addresses for specific devices."
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to continue?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
check_configs
ask_for_subnet
fi
}
check_configs()
{
NUMCONFIGS=`ls /etc/dhcp/conf.d/ | wc -l`
if [ ! -d /etc/dhcp/conf.d/ ] || [ $NUMCONFIGS -lt 1 ]; then
msgbox "There is no subnet configured yet. Please run DHCP Server config first."
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to install and configure a DHCP server now?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
. $HOMEDIR/dhcp_server.sh
# TODO probably better to do a rerun of check_configs here instead of quiting?
return 0
fi
fi
}
ask_for_subnet()
{
OPTIONS=""
OIFS="$IFS"
IFS="$(printf '\n\t')"
for files in `find /etc/dhcp/conf.d -type f`
do
ID=`echo $files | cut -d "." -f3`
SUBNET=`grep "subnet" $files | cut -d "{" -f1 | sed "s/.$//"`
OPTIONS="$ID $SUBNET
$OPTIONS"
done
CC=$(whiptail --backtitle "$TITLE" --menu "Select Subnet to reconfigure" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
$OPTIONS \
3>&1 1>&2 2>&3)
IFS="$OIFS"
if [ $? -eq 0 ]; then
FILE=/etc/dhcp/conf.d/config.$CC
ask_for_task
else
return 0
fi
}
ask_for_task()
{
CC=$(whiptail --backtitle "$TITLE" --menu "What do you want to change?" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
"1" "Reconfigure subnet" \
"2" "Reconfigure IP-Range" \
"3" "Reconfigure Default Gateway" \
"4" "Reconfigure DNS Servers" \
"5" "Reconfigure Search-Domain" \
"6" "Configure devices with static IPs" \
"7" "Configure autoregistration on DNS Server (Bind9 only)" \
"8" "Remove subnet" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case "$CC" in
"1") change_subnet ;;
"2") change_range ;;
"3") change_gateway ;;
"4") change_dns ;;
"5") change_search ;;
"6") change_static ;;
"7") change_autodns ;;
"8") remove_subnet ;;
*) msgbox "Error 002. Please report on the forums" && exit 0 ;;
esac || msgbox "I don't know how you got here! >> $CC << Report on the forums"
else
return 0
fi
}
change_subnet()
{
CURRENT_SUBNET=`grep "subnet" $FILE | cut -d "{" -f1 | sed "s/.$//"`
SUBNET=$(whiptail --backtitle "$TITLE" --title "Configure Subnet (Subnet IP / Netmask)" --inputbox "Change the values as they seem fitting for you." 0 40 "$CURRENT_SUBNET" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
sed -i "s/$CURRENT_SUBNET/$SUBNET/" $FILE
restart_server
ask_for_task
fi
}
change_range()
{
CURRENT_RANGE=`cat $FILE | grep range | sed 's/[^0-9\.\ ]*//g' | sed 's/^ //'`
DHCP_RANGE=$(whiptail --backtitle "$TITLE" --title "Subnet IP-Range" --inputbox "IP-Range" 0 40 "$CURRENT_RANGE" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
sed -i "s/range.*/range $DHCP_RANGE;/" $FILE
restart_server
ask_for_task
fi
}
change_gateway()
{
CURRENT_GATEWAY=`cat $FILE | grep routers | sed 's/option routers//' | sed 's/;//' | sed 's/\t//' | sed 's/^ //'`
GATEWAY=$(whiptail --backtitle "$TITLE" --title "Gateway Server" --inputbox "Gateway Server (Router-IP). Multiple Gateways can be separated by comma \",\"" 0 20 "$CURRENT_GATEWAY" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
if [ "x$CURRENT_GATEWAY" != "x" ]; then
sed -i "s/option routers.*/option routers $GATEWAY;/" $FILE
else
sed -i "/# RANGE END/a# ROUTER START\n option routers $GATEWAY;\n# ROUTER END" $FILE
fi
restart_server
ask_for_task
fi
}
change_dns()
{
CURRENT_DNS=`cat $FILE | grep domain-name-servers | sed 's/option domain-name-servers//' | sed 's/;//' | sed 's/\t//' | sed 's/^ //'`
DNS=$(whiptail --backtitle "$TITLE" --inputbox "DNS Server (separate multiple DNS by comma \",\")" 0 20 "$CURRENT_DNS" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
if [ "x$CURRENT_DNS" != "x" ]; then
sed -i "s/option domain-name-servers.*/option domain-name-servers $DNS;/" $FILE
else
sed -i "/# RANGE END/a# DNS START\n option domain-name-servers $DNS;\n# DNS END" $FILE
fi
restart_server
ask_for_task
fi
}
change_search()
{
CURRENT_SEARCH=`cat $FILE | grep "domain-name " | sed 's/option domain-name //' | sed 's/;//' | sed 's/\t//' | sed 's/"//g'`
DOMAIN=$(whiptail --backtitle "$TITLE" --inputbox "Search Domain" 0 20 "$CURRENT_SEARCH" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
if [ "x$CURRENT_SEARCH" != "x" ]; then
sed -i "s/option domain-name .*/option domain-name \"$DOMAIN\";/" $FILE
else
sed -i "/# RANGE END/a# DOMAIN START\n option domain-name \"$DOMAIN\";\n# DOMAIN END" $FILE
fi
restart_server
ask_for_task
fi
}
change_static()
{
CC=$(whiptail --backtitle "$TITLE" --menu "What do you want to change?" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
"1" "Add new device with static IP address" \
"2" "Remove device with static IP address" \
"3" "Modify a device with static IP address" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case "$CC" in
"1") add_static ;;
"2") remove_static ;;
"3") modify_static ;;
*) msgbox "Error 003. Please report on the forums" && exit 0 ;;
esac || msgbox "I don't know how you got here! >> $CC << Report on the forums"
else
ask_for_task
fi
}
add_static()
{
MAC=$(whiptail --backtitle "$TITLE" --inputbox "MAC-Address for system with static IP (format aa:bb:cc:dd:ee:ff):" 0 20 "" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ] && [ "x$MAC" != "x" ]; then
IP=$(whiptail --backtitle "$TITLE" --inputbox "Static IP for new device:" 0 20 "" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ] && [ "x$IP" != "x" ]; then
NAME=$(whiptail --backtitle "$TITLE" --inputbox "Hostname of the device (will be used as an identifier):" 0 40 "" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ] && [ "x$NAME" != "x" ]; then
# TODO add hostname in configuration?
sed -i "/# RANGE END/a# HOST $NAME START\nhost $NAME {\n hardware ethernet $MAC;\n fixed-address $IP;\n}\n# HOST $NAME END" $FILE
else
ask_for_task
fi
else
ask_for_task
fi
else
ask_for_task
fi
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to add another device?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
add_static
else
restart_server
ask_for_task
fi
}
get_static()
{
OPTIONS=""
NUM_DEVICES=`grep "^host" $FILE | wc -l`
i=0
if [ $NUM_DEVICES -ge 1 ]; then
for DEVICE in `grep "^host" $FILE | sed "s/^host //" | sed "s/ {//"`
do
# TODO find menu with only value without label
OPTIONS="$DEVICE $(($NUM_DEVICES-$i))
$OPTIONS"
i=$(($i+1))
done
else
msgbox "No devices with static IPs found in this subnet"
ask_for_task
fi
}
remove_static()
{
get_static
CC=$(whiptail --backtitle "$TITLE" --menu "Select Device to remove:" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
$OPTIONS \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
START=`grep -n "^# HOST $CC START" $FILE | cut -d ":" -f1`
END=`grep -n "^# HOST $CC END" $FILE | cut -d ":" -f1`
sed -i "${START},${END}d" $FILE
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to remove another device?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
remove_static
else
restart_server
ask_for_task
fi
else
ask_for_task
fi
}
modify_static()
{
# TODO put restart somewhere
get_static
DEVICE=$(whiptail --backtitle "$TITLE" --menu "Select Device to modify:" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
$OPTIONS \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
CC=$(whiptail --backtitle "$TITLE" --menu "What do you want to modify?" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
"1" "Change name/identifyer" \
"2" "Change MAC address" \
"3" "Change IP address" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case "$CC" in
"1") change_name_static ;;
"2") change_mac_static ;;
"3") change_ip_static ;;
*) msgbox "Error 004. Please report on the forums" && exit 0 ;;
esac || msgbox "I don't know how you got here! >> $CC << Report on the forums"
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to modify another device?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
modify_static
fi
else
ask_for_task
fi
else
ask_for_task
fi
}
change_name_static()
{
NAME=$(whiptail --backtitle "$TITLE" --inputbox "New hostname of the device (will be used as an identifier):" 0 40 "$DEVICE" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ] && [ "x$NAME" != "x" ]; then
sed -i "s/^# HOST $DEVICE START/# HOST $NAME START/" $FILE
sed -i "s/^# HOST $DEVICE END/# HOST $NAME END/" $FILE
sed -i "s/^host $DEVICE {/host $NAME {/" $FILE
else
msgbox "Nothing will be changed."
fi
}
change_mac_static()
{
OLD_MAC=`grep -a3 "^# HOST $DEVICE START" $FILE | grep "hardware ethernet" | sed "s/hardware ethernet//" | sed "s/\t//g" | sed "s/ //g" | sed "s/;//"`
MAC=$(whiptail --backtitle "$TITLE" --inputbox "New MAC-Address for system with static IP (format aa:bb:cc:dd:ee:ff):" 0 20 "$OLD_MAC" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ] && [ "x$MAC" != "x" ]; then
sed -i "s/hardware ethernet $OLD_MAC/hardware ethernet $MAC/" $FILE
else
msgbox "Nothing will be changed."
fi
}
change_ip_static()
{
OLD_IP=`grep -a3 "^# HOST $DEVICE START" $FILE | grep "fixed-address" | sed "s/fixed-address//" | sed "s/\t//g" | sed "s/ //g" | sed "s/;//"`
IP=$(whiptail --backtitle "$TITLE" --inputbox "New static IP for new device:" 0 20 "$OLD_IP" --cancel-button "Exit" --ok-button "Select" 3>&1 1>&2 2>&3)
if [ $? -eq 0 ] && [ "x$IP" != "x" ]; then
sed -i "s/fixed-address $OLD_IP/fixed-address $IP/" $FILE
else
msgbox "Nothing will be changed."
fi
}
change_autodns()
{
# get all available DNS zones (bind9 only)
# sanity check if bind9 is even installed
if [ -f /usr/sbin/named ]; then
CC=$(whiptail --backtitle "$TITLE" --menu "What do you want to modify?" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
"1" "Activated automatic dns registrytion for a special DNS-Zone" \
"2" "Deactivate automatic dns registrytion for a special DNS-Zone" \
3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
case "$CC" in
"1") enable_autodns ;;
"2") disable_autodns ;;
*) msgbox "Error 005. Please report on the forums" && exit 0 ;;
esac || msgbox "I don't know how you got here! >> $CC << Report on the forums"
fi
else
disable_autodns
fi
}
enable_autodns()
{
# make sure DHCP is configured to allow autodns
sed -i "s/ddns-update-style.*/ddns-update-style interim;/" /etc/dhcp/dhcpd.conf
sed -i "s/^#authoritative;/authoritative;/" /etc/dhcp/dhcpd.conf
# TODO check if /etc/bind/rndc.key exists
if [ `grep "include \"/etc/bind/rndc.key\"" /etc/dhcp/dhcpd.conf | wc -l` -lt 1 ]; then
# make sure to include rndc.key BEFORE calling any config files
if [ `grep "^include \"/etc/bind/rndc.key\"" /etc/dhcp/dhcpd.conf | wc -l` -lt 1 ]; then
if [ `grep "/etc\/dhcp\/conf.d\/config.*" | wc -l` -ge 1 ]; then
sed -i "\/etc\/dhcp\/conf.d\/config.*/iinclude \"/etc/bind/rndc.key\";/" /etc/dhcp/dhcpd.conf
else
echo "include \"/etc/bind/rndc.key\";" >> /etc/dhcp/dhcpd.conf
fi
fi
fi
msgbox "Please Select the DNS-ZONE you want to add to $SUBNET"
select_dnszone
# try to find DNS zone in subnet
if [ `grep $DNSZONE $FILE | wc -l` -ge 1 ]; then
msg "The ZONE is already activated"
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to activate another zone?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
enable_autodns
else
ask_for_task
fi
else
ENTRY="zone `echo $DNSZONE | sed "s/^db.//"`. { \\
primary 127.0.0.1; \\
key \"rndc-key\" \\
}"
sed -i "/# RANGE END/a# ZONE `echo $DNSZONE | sed "s/^db.//"` START\n$ENTRY\n# ZONE `echo $DNSZONE | sed "s/^db.//"` END" $FILE
restart_server
fi
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to activate another zone?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
enable_autodns
else
ask_for_task
fi
}
disable_autodns()
{
OPTIONS=""
NUM_ZONES=$((`grep "^# ZONE" $FILE | wc -l`/2))
if [ $NUM_ZONES -ge 1 ]; then
i=0
for zones in `grep "^# ZONE" $FILE | grep "START" | sed "s/# ZONE //" | sed "s/ START//"`
do
OPTIONS="$zones $(($NUM_ZONES-$i))
$OPTIONS"
i=$(($i+1))
done
DNSZONE=$(whiptail --backtitle "$TITLE" --menu "Select DNS-Zone to remove:" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
$OPTIONS \
3>&1 1>&2 2>&3)
if [ $? -eq 1 ]; then
msgbox "Aborting as requested"
else
START=`grep -n "^# ZONE $DNSZONE START" $FILE | cut -d ":" -f1`
END=`grep -n "^# ZONE $DNSZONE END" $FILE | cut -d ":" -f1`
sed -i "${START},${END}d" $FILE
msgbox "Automatic DNS updated for DNS-Zone \"$DNSZONE\" was removed."
restart_server
CC=$(whiptail --backtitle "$TITLE" --yesno "Do you want to remove another zone?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
disable_autodns
else
ask_for_task
fi
fi
else
msgbox "No zones configured for automatic dns updates"
ask_for_task
fi
}
select_dnszone()
{
OPTIONS=""
OIFS="$IFS"
IFS="$(printf '\n\t')"
NUM_FILES=`find /etc/bind/conf.d -type f -name "db.*" | wc -l`
i=0
for files in `find /etc/bind/conf.d -type f -name "db.*"`
do
OPTIONS="`basename $files` $(($NUM_FILES-$i))
$OPTIONS"
i=$(($i+1))
done
DNSZONE=$(whiptail --backtitle "$TITLE" --menu "Select DNS-Zone:" 0 0 1 --cancel-button "Exit" --ok-button "Select" \
$OPTIONS \
3>&1 1>&2 2>&3)
if [ $? -eq 1 ]; then
IFS="$OIFS"
msgbox "Aborting as requested"
return 0
fi
IFS="$OIFS"
}
remove_subnet()
{
CC=$(whiptail --backtitle "$TITLE" --yesno "Are you sure you want to completely remove the subnet?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
rm -f $FILE
DELETE=`grep -n "$FILE" "/etc/dhcp/dhcpd.conf" | cut -d ":" -f1`
sed -i "${DELETE}d" /etc/dhcp/dhcpd.conf
restart_server
ask_for_task
fi
}
restart_server()
{
CC=$(whiptail --backtitle "$TITLE" --yesno "DHCP Server configuration changed, do you want to restart server now?" 0 0 3>&1 1>&2 2>&3)
if [ $? -eq 0 ]; then
service isc-dhcp-server restart
fi
}
intro