[5133823] | 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"} |
---|
[bbf5371] | 18 | SERVERCONF=$OPENGNSYS/etc/ogserver.cfg |
---|
[5133823] | 19 | DHCPCONF=/etc/dhcp/dhcpd.conf |
---|
| 20 | DHCPCONFBCK="$DHCPCONF-$(date +"%Y%m%d")" |
---|
| 21 | |
---|
| 22 | source $OPENGNSYS/lib/ogfunctions.sh || exit 1 |
---|
| 23 | |
---|
[da8fdfc] | 24 | # Show help or version number. |
---|
[5133823] | 25 | [ "$*" == "help" ] && help |
---|
[da8fdfc] | 26 | [ "$*" == "version" ] && version |
---|
[5133823] | 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 |
---|
[b897e4e] | 49 | RESOURCES="$*" |
---|
[5133823] | 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 | |
---|
[b897e4e] | 54 | [ "$*" ] && WHEREEXPR="WHERE $(sed -e "s/\('[^']*'\)/nombreaula=\1 OR nombreordenador=\1 OR/g" <<< "$*")" |
---|
[5133823] | 55 | WHEREEXPR="${WHEREEXPR% OR}" |
---|
| 56 | |
---|
| 57 | # Looking for data. |
---|
| 58 | SEDEXPR="" |
---|
| 59 | while 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 |
---|
[da8fdfc] | 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 |
---|
[5133823] | 69 | # Find any "host" clause. |
---|
| 70 | SEDEXPR+="/\bhost $NAME\b/" |
---|
[da8fdfc] | 71 | if ! grep -Eq "host[[:space:]]+$NAME[[:space:]]*}" $DHCPCONF; then |
---|
[5133823] | 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 | " |
---|
| 84 | done <<<$(dbexec " |
---|
| 85 | SELECT 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" |
---|
| 96 | cp -a $DHCPCONF $DHCPCONFBCK || raiseError access "Cannot back-up DHCP configuration file" |
---|
| 97 | sed -i -re "$SEDEXPR" $DHCPCONF |
---|
| 98 | # Delete duplicate empty lines. |
---|
| 99 | perl -0777pi -e "s/\n{3,}/\n\n/g" $DHCPCONF |
---|
| 100 | # Restart the service, if needed. |
---|
| 101 | [ "$RESTART" ] && restart isc-dhcp-server |
---|
| 102 | |
---|