1 | #!/bin/bash |
---|
2 | #@file addtodhcp |
---|
3 | #@brief Append a "host" section for each defined computer to the DHCP configuration file. |
---|
4 | #@usage addtodhcp [-f FILE] [-r] [-e] [ {LABNAME|COMPUTERNAME} ...] |
---|
5 | #@param -f, --file FILE DHCP configuration file (/etc/dhcp/dhcpd.conf, by default) |
---|
6 | #@param -r, --restart restart DHCP service |
---|
7 | #@param -e, --exam assign to alternative network ("exam mode" from Universidad de Sevilla) |
---|
8 | #@param LABNAME only add computers defined in this lab |
---|
9 | #@param COMPUTERNAME only add a single computer data |
---|
10 | #@version 1.1.1b - Initial version. |
---|
11 | #@author Ramón M. Gómez - ETSII Univ. Sevilla |
---|
12 | #@date 2020-02-03 |
---|
13 | |
---|
14 | |
---|
15 | # Variables. |
---|
16 | PROG="$(basename "$0")" |
---|
17 | OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} |
---|
18 | SERVERCONF=$OPENGNSYS/etc/ogAdmServer.cfg |
---|
19 | DHCPCONF=/etc/dhcp/dhcpd.conf |
---|
20 | DHCPCONFBCK="$DHCPCONF-$(date +"%Y%m%d")" |
---|
21 | |
---|
22 | source $OPENGNSYS/lib/ogfunctions.sh || exit 1 |
---|
23 | |
---|
24 | # Show help or version number. |
---|
25 | [ "$*" == "help" ] && help |
---|
26 | [ "$*" == "version" ] && version |
---|
27 | # Error control. |
---|
28 | [ "$USER" != "root" ] && raiseError access "Need to be root" |
---|
29 | source $SERVERCONF 2>/dev/null || raiseError access "Cannot read OpenGnsys Server configuration file" |
---|
30 | |
---|
31 | # Processing parameters. |
---|
32 | opts=$(getopt -n "$PROG" -l exam,file:,restart -o 'ef:r' -- "$@" ) || raiseError usage |
---|
33 | set -- $opts |
---|
34 | while [ "$1" ]; do |
---|
35 | case "$1" in |
---|
36 | -e|--exam) |
---|
37 | EXAM=1 |
---|
38 | shift ;; |
---|
39 | -f|--file) |
---|
40 | eval DHCPCONF=$2 |
---|
41 | shift 2 ;; |
---|
42 | -r|--restart) |
---|
43 | RESTART=1 |
---|
44 | shift ;; |
---|
45 | --) |
---|
46 | shift; break ;; |
---|
47 | esac |
---|
48 | done |
---|
49 | RESOURCES="$*" |
---|
50 | [ -f $DHCPCONF ] || raiseError access "Cannot access DHCP configuration file" |
---|
51 | grep -q "^[ ]*\bsubnet\b" $DHCPCONF || raiseError access "Cannot detect any \"group\" clauses in DHCP configuration file" |
---|
52 | grep -q "^[ ]*\bgroup\b" $DHCPCONF && raiseError access "Cannot modify DHCP configuration file with \"group\" clauses" |
---|
53 | |
---|
54 | [ "$*" ] && WHEREEXPR="WHERE $(sed -e "s/\('[^']*'\)/nombreaula=\1 OR nombreordenador=\1 OR/g" <<< "$*")" |
---|
55 | WHEREEXPR="${WHEREEXPR% OR}" |
---|
56 | |
---|
57 | # Looking for data. |
---|
58 | SEDEXPR="" |
---|
59 | while read -pe NAME IP MAC ROUTER LAB; do |
---|
60 | echolog "Just read these from the database: NAME ($NAME) IP ($IP) MAC ($MAC) ROUTER ($ROUTER) LAB ($LAB)" |
---|
61 | [ "$LAB" ] || break |
---|
62 | if [ "$EXAM" ]; then |
---|
63 | IP="${IP/10.1./192.168.}" |
---|
64 | ROUTER="${ROUTER/10.1./192.168.}" |
---|
65 | fi |
---|
66 | # Check if router is defined. |
---|
67 | if ! grep -Eq "routers[[:space:]]+$ROUTER" $DHCPCONF; then |
---|
68 | raiseError notfound "Router \"$ROUTER\" not defined in DHCP configuration file" |
---|
69 | fi |
---|
70 | # Find any "host" clause. |
---|
71 | SEDEXPR+="/\bhost $NAME\b/" |
---|
72 | if ! grep -Eq "host[[:space:]]+$NAME[[:space:]]*}" $DHCPCONF; then |
---|
73 | SEDEXPR+=",/}/" |
---|
74 | fi |
---|
75 | if [ "$LAB" != "$LABBCK" ]; then |
---|
76 | NEWLAB="\\\n" |
---|
77 | LABBCK="$LAB" |
---|
78 | else |
---|
79 | NEWLAB="" |
---|
80 | fi |
---|
81 | # Delete the found "host" clause and add a new one. |
---|
82 | SEDEXPR+="d |
---|
83 | /^[[:space:]]*option[[:space:]]+routers[[:space:]]+\b$ROUTER\b/a ${NEWLAB}host $NAME { hardware ethernet $MAC; fixed-address $IP; } # $LAB |
---|
84 | " |
---|
85 | done <<<$(dbexec " |
---|
86 | SELECT nombreordenador, ip, |
---|
87 | CONCAT_WS('', SUBSTR(mac, 1, 2), ':', SUBSTR(mac, 3, 2), ':', SUBSTR(mac, 5, 2), ':', |
---|
88 | SUBSTR(mac, 7, 2), ':', SUBSTR(mac, 9, 2), ':', SUBSTR(mac, 11, 2)), |
---|
89 | ordenadores.router, nombreaula |
---|
90 | FROM ordenadores |
---|
91 | JOIN aulas USING (idaula) |
---|
92 | $WHEREEXPR |
---|
93 | ORDER BY nombreaula ASC, idordenador ASC;" 2>/dev/null) |
---|
94 | |
---|
95 | # Edit DHCP configuration file. |
---|
96 | [ "$SEDEXPR" ] || raiseError notfound "$RESOURCES" |
---|
97 | cp -a $DHCPCONF $DHCPCONFBCK || raiseError access "Cannot back-up DHCP configuration file" |
---|
98 | echolog "Sed expression: ($SEDEXPR)" |
---|
99 | sed -i -re "$SEDEXPR" $DHCPCONF |
---|
100 | # Delete duplicate empty lines. |
---|
101 | perl -0777pi -e "s/\n{3,}/\n\n/g" $DHCPCONF |
---|
102 | # Restart the service, if needed. |
---|
103 | [ "$RESTART" ] && echolog "Restarting DHCP server" && restart isc-dhcp-server |
---|
104 | |
---|