source: server/bin/addtodhcp

qndtest
Last change on this file was b897e4e, checked in by Ramón M. Gómez <ramongomez@…>, 4 years ago

#955: Correct quoting of input parameters.

  • Property mode set to 100755
File size: 3.6 KB
Line 
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.
16PROG="$(basename "$0")"
17OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"}
18SERVERCONF=$OPENGNSYS/etc/ogAdmServer.cfg
19DHCPCONF=/etc/dhcp/dhcpd.conf
20DHCPCONFBCK="$DHCPCONF-$(date +"%Y%m%d")"
21
22source $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"
29source $SERVERCONF 2>/dev/null || raiseError access "Cannot read OpenGnsys Server configuration file"
30
31# Processing parameters.
32opts=$(getopt -n "$PROG" -l exam,file:,restart -o 'ef:r' -- "$@" ) || raiseError usage
33set -- $opts
34while [ "$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
48done
49RESOURCES="$*"
50[ -f $DHCPCONF ] || raiseError access "Cannot access DHCP configuration file"
51grep -q "^[     ]*\bsubnet\b" $DHCPCONF || raiseError access "Cannot detect any \"group\" clauses in DHCP configuration file"
52grep -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" <<< "$*")"
55WHEREEXPR="${WHEREEXPR% OR}"
56
57# Looking for data.
58SEDEXPR=""
59while read -pe NAME IP MAC ROUTER LAB; do
60    [ "$LAB" ] || break
61    if [ "$EXAM" ]; then
62        IP="${IP/10.1./192.168.}"
63        ROUTER="${ROUTER/10.1./192.168.}"
64    fi
65    # Check if router is defined.
66    if ! grep -Eq "routers[[:space:]]+$ROUTER" $DHCPCONF; then
67        raiseError notfound "Router \"$ROUTER\" not defined in DHCP configuration file"
68    fi
69    # Find any "host" clause.
70    SEDEXPR+="/\bhost $NAME\b/"
71    if ! grep -Eq "host[[:space:]]+$NAME[[:space:]]*}" $DHCPCONF; then
72        SEDEXPR+=",/}/"
73    fi
74    if [ "$LAB" != "$LABBCK" ]; then
75        NEWLAB="\\\n"
76        LABBCK="$LAB"
77    else
78        NEWLAB=""
79    fi
80    # Delete the found "host" clause and add a new one.
81    SEDEXPR+="d
82/^[[:space:]]*option[[:space:]]+routers[[:space:]]+\b$ROUTER\b/a ${NEWLAB}host $NAME { hardware ethernet $MAC; fixed-address $IP; }  # $LAB
83"
84done <<<$(dbexec "
85SELECT nombreordenador, ip,
86       CONCAT_WS('', SUBSTR(mac, 1, 2), ':', SUBSTR(mac, 3, 2), ':', SUBSTR(mac, 5, 2), ':',
87                     SUBSTR(mac, 7, 2), ':', SUBSTR(mac, 9, 2), ':', SUBSTR(mac, 11, 2)),
88       ordenadores.router, nombreaula
89  FROM ordenadores
90  JOIN aulas USING (idaula)
91 $WHEREEXPR
92 ORDER BY nombreaula ASC, idordenador ASC;" 2>/dev/null)
93
94# Edit DHCP configuration file.
95[ "$SEDEXPR" ] || raiseError notfound "$RESOURCES"
96cp -a $DHCPCONF $DHCPCONFBCK || raiseError access "Cannot back-up DHCP configuration file"
97sed -i -re "$SEDEXPR" $DHCPCONF
98# Delete duplicate empty lines.
99perl -0777pi -e "s/\n{3,}/\n\n/g" $DHCPCONF
100# Restart the service, if needed.
101[ "$RESTART" ] && restart isc-dhcp-server
102
Note: See TracBrowser for help on using the repository browser.