#!/bin/bash set -e PROGNAME=$(basename $0) # $HeadURL: http://sos16-vip/svn/kickstart/trunk/bin/defaults-dumper $ $LastChangedRevision: 669 $ # Load libraries . $(dirname $0)/../include/mkdi.sh main() { local TARGET_HOSTNAME TARGET_MACADDR TARGET_IPADDR # Defaults for options VERBOSELEVEL=2 # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-macaddr=*) TARGET_MACADDR=${1#*=} ;; # General options --help|-h) usage 0 ;; --verbose|-v) VERBOSELEVEL=3 ;; -d) VERBOSELEVEL=$2; shift ;; --debug=*) VERBOSELEVEL=${1#*=} ;; --) shift; break ;; -*) usage ;; *) break ;; esac shift done # Process arguments [ $# -ge 1 ] || usage MODE=$1; shift debug 10 "main: MODE=$MODE" # Guts case $MODE in create) create --target-hostname=$TARGET_HOSTNAME --target-macaddr=$TARGET_MACADDR --target-ipaddr=$TARGET_IPADDR "$@" || return $? ;; delete) delete --target-hostname=$TARGET_HOSTNAME --target-macaddr=$TARGET_MACADDR --target-ipaddr=$TARGET_IPADDR "$@" || return $? ;; list) list "$@" || return $? ;; purge) purge "$@" || return $? ;; *) usage ;; esac return 0 } create() { local TARGET_HOSTNAME TARGET_MACADDR TARGET_IPADDR # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-macaddr=*) TARGET_MACADDR=${1#*=} ;; # General options --) shift; break ;; -*) internal "create: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 0 ] || internal "create: $#: wrong arg count" # Create entry info "creating DHCP reservation ..." create_or_delete --target-hostname=$TARGET_HOSTNAME --target-macaddr=$TARGET_MACADDR --target-ipaddr=$TARGET_IPADDR create || return $? return 0 } delete() { local TARGET_HOSTNAME TARGET_MACADDR TARGET_IPADDR # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-macaddr=*) TARGET_MACADDR=${1#*=} ;; # General options --) shift; break ;; -*) internal "delete: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 0 ] || internal "delete: $#: wrong arg count" # Delete entry info "deleting DHCP reservation ..." create_or_delete --target-hostname=$TARGET_HOSTNAME --target-macaddr=$TARGET_MACADDR --target-ipaddr=$TARGET_IPADDR delete || return $? return 0 } create_or_delete() { local TARGET_HOSTNAME TARGET_MACADDR TARGET_IPADDR local MODE LINE DHCPD_OUTPUT # Process options while [ "X$1" != X ]; do case $1 in # Application specific options --target-hostname=*) TARGET_HOSTNAME=${1#*=} ;; --target-ipaddr=*) TARGET_IPADDR=${1#*=} ;; --target-macaddr=*) TARGET_MACADDR=${1#*=} ;; # General options --) shift; break ;; -*) internal "create_or_delete: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 1 ] || internal "create_or_delete: $#: wrong arg count" MODE=$1 # Sanity checks and derivations validate_vars MODE TARGET_HOSTNAME TARGET_IPADDR TARGET_MACADDR || return $? debug 10 "main: MODE=$MODE, TARGET_HOSTNAME=$TARGET_HOSTNAME, TARGET_IPADDR=$TARGET_IPADDR, TARGET_MACADDR=$TARGET_MACADDR" LINE=" host $TARGET_HOSTNAME { hardware ethernet $TARGET_MACADDR; fixed-address $TARGET_IPADDR; } # $TARGET_HOSTNAME" # Guts if [ $MODE = create ]; then ! grep " host $TARGET_HOSTNAME " /etc/dhcp3/dhcpd.conf > /dev/null || { error "$TARGET_HOSTNAME: entry present already (do you need to run 'mkdi delete $TARGET_HOSTNAME' first?)"; return 1; } debug 10 "create_or_delete: creating entry ..." perl -pi -e "s/^( # INSTALL-SERVER ENTRIES START HERE)\$/\$1\n$LINE/" /etc/dhcp3/dhcpd.conf elif [ $MODE = delete ]; then debug 10 "create_or_delete: deleting entry ..." perl -0777 -pi -e "s/^( # INSTALL-SERVER ENTRIES START HERE\n.*?)^$LINE\n(.*?^ # INSTALL-SERVER ENTRIES END HERE)/\$1\$2/msg" /etc/dhcp3/dhcpd.conf fi DHCPD_OUTPUT=$(/etc/init.d/dhcp3-server restart 2>&1) || { error "dhcpd: failed (output was: $DHCPD_OUTPUT)"; return 1; } return 0 } purge() { # Guts perl -0777 -pi -e "s/^( # INSTALL-SERVER ENTRIES START HERE\n)^.*?\n(^ # INSTALL-SERVER ENTRIES END HERE)/\$1\$2/msg" /etc/dhcp3/dhcpd.conf DHCPD_OUTPUT=$(/etc/init.d/dhcp3-server restart 2>&1) || { error "dhcpd: failed (output was: $DHCPD_OUTPUT)"; return 1; } return 0 } list() { # Process options while [ "X$1" != X ]; do case $1 in # Application specific options # General options --) shift; break ;; -*) internal "list: $1: invalid option" ;; *) break ;; esac shift done # Process arguments [ $# = 0 ] || internal "list: $#: wrong arg count" # Guts sed -n "s/^ *\(.*; } #.*\)/\1/p" /etc/dhcp3/dhcpd.conf return 0 } usage() { local RC RC=${1:-1} { echo "Usage: $PROGNAME [ ] { create | delete | list }" echo } | if [ $RC = 0 ]; then cat else cat >&2 fi exit $RC } main "$@"