#!/bin/sh function saveFile { file=$1 save=$1.save dhcp=$1.dhcp test -f $dhcp || return 0 test -f $save || cp $file $save mv $dhcp $file return 0 } function restoreFile { file=$1 save=$1.save test -f $save && mv $save $file return 0 } interface=$1 HostFile="/etc/hosts" DHCPHostFile="${HostFile}.dhcp" MYCURRENTHOSTNAME=$(/bin/hostname) MYCURRENTDOMAIN=$(/bin/dnsdomainname) case $0 in *ifup*) echo "Trying to obtain network configuration via DHCP." /sbin/dhcpcd -HD $interface >/dev/null || { echo "No response from DHCP server" >&2 exit 1 } if test -f /var/run/dhcpcd-$interface.info; then . /var/run/dhcpcd-$interface.info if test -n "$NISDOMAIN"; then cat >/etc/nis.conf.dhcp <<-EOF # # Generated by DHCP boot script # EOF if test -n "$NISSERVER"; then echo "domainname $NISDOMAIN server $NISSERVER" else echo "domainname $NISDOMAIN broadcast" fi >> /etc/nis.conf.dhcp fi ################################################################################ # # Update /etc/hosts file with Hostname, DomainName, and IPAddress from DHCP # # Assumes that hosts file is in the format : # # <<<>>> # # And that IPADDR, HOSTNAME and DOMAIN are configure by DHCP. # # NOTES: # 1. The Update Logic will handle the situation where the # DHCP Server returns a Fully Qualified Domain Name # for the HOSTNAME. From the FQDN, the logic generates # a Short HOSTNAME that is compatible with the # /etc/hosts notation. # 2. The Update Logic will not modify the /etc/hosts file # if a duplicate entry exists. # 3. The Update Logic will use the current HOSTNAME if the # DHCP Server does not provide a HOSTNAME Setting. # 4. The Update Logic will use the current DNS DOMAIN Name # if the DHCP Server does not provide DOMAIN NAME # Setting. # # *** WARNING **** With respect to Items 3 and 4, this logic # may be a problem on Multi-Homed Systems (i.e. Multiple NICs # with multiple Subnet Connections). To disable this logic, # modify the below "INHIBITDEFAULTS" variable to "TRUE", # otherwise, set to "FALSE". # # ################################################################################ INHIBITDEFAULTS="FALSE" if [ -z "${HOSTNAME}" ] && [ -n "${MYCURRENTHOSTNAME}" ] && [ ${INHIBITDEFAULTS} = "FALSE" ] then HOSTNAME=${MYCURRENTHOSTNAME} fi if [ -z "${DOMAIN}" ] && [ -n "${MYCURRENTDOMAIN}" ] && [ ${INHIBITDEFAULTS} = "FALSE" ] then DOMAIN=${MYCURRENTDOMAIN} fi if [ -n "${IPADDR}" ] && [ -n "${HOSTNAME}" ] && [ -n "${DOMAIN}" ] then /usr/bin/awk -v a="${IPADDR}" -v b="${HOSTNAME}" -v c="${DOMAIN}" ' BEGIN { IPADDR = a HOSTIDX = match(b,/[[:punct:]]/) if (HOSTIDX == 0) { HOSTNAME = b } else { HOSTNAME = substr(b,1,HOSTIDX-1) } DOMAIN = c FQDN = sprintf("%s.%s", HOSTNAME, DOMAIN) MATCHENTRY = 0 } { if((substr($0,1, 1) == "#") || (($1 != IPADDR) && ($2 != FQDN) && ($3 !=HOSTNAME))) { print $0 } else { if (($1 == IPADDR) && ($2 == FQDN) && ($3 == HOSTNAME)) { MATCHENTRY=1 print $0 } else { printf ("# Commented by DHCPCD configscripts: %s\n", $0) } } } END { if (MATCHENTRY == 0) { print "#The following entry added by DHCPCD configuration scripts" printf("%s\t%s\t%s\n",IPADDR,FQDN,HOSTNAME) } }' ${HostFile} > ${DHCPHostFile} saveFile ${HostFile} fi fi saveFile /etc/nis.conf saveFile /etc/resolv.conf : ;; *ifdown*) echo "Shutting down DHCP daemon." /sbin/dhcpcd -k $interface restoreFile /etc/nis.conf restoreFile /etc/resolv.conf restoreFile ${HOSTFILE} : ;; *) echo "$0: unknown mode of operation for DHCP script" >&2 exit 1;; esac exit 0