SourceForge.net Logo

D.8. /etc/sysconfig/network/udhcpc.script

#!/bin/ash
# 
# GPL $Id: udhcpc.script,v 1.1 2005/10/09 16:39:06 cvonk Exp $
# run-time script called by the DHCP client (udhcpd) when it handled an event

RESOLV_CONF=/etc/sysconfig/dnsmasq/resolv.conf
NTPSERVER_CONF=/etc/sysconfig/ntpserver.conf

error_exit() {
    echo "udhcpc.script: error: $@" >&2; exit 1
}

handle_bound() {
    IPADDR=`echo $ip | awk '{print $1 }'`
    case `echo $subnet | awk '{ print $1 }'` in
        255.255.0.0)     NETMASKBITS="16" ;;
        255.255.128.0)   NETMASKBITS="17" ;;
        255.255.192.0)   NETMASKBITS="18" ;;
        255.255.224.0)   NETMASKBITS="19" ;;
        255.255.240.0)   NETMASKBITS="20" ;;
        255.255.248.0)   NETMASKBITS="21" ;;
        255.255.252.0)   NETMASKBITS="22" ;;
        255.255.254.0)   NETMASKBITS="23" ;;
        255.255.255.0)   NETMASKBITS="24" ;;
        255.255.255.128) NETMASKBITS="25" ;;
        255.255.255.192) NETMASKBITS="26" ;;
        255.255.255.224) NETMASKBITS="27" ;;
        255.255.255.240) NETMASKBITS="28" ;;
        255.255.255.248) NETMASKBITS="29" ;;
	*) NETMASKBITS= ;;
    esac
    [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
	
#   echo "udhcpc.script: ip=$IPADDR/$NETMASKBITS gw=$router dns=$dns"

    if ! arping -q -c 2 -w 3 -D -I $interface $IPADDR ; then
	error_exit "another host already uses address $IPADDR on $interface."
    fi

    if ! ip address add $IPADDR/$NETMASKBITS brd + dev $interface; then
	error_exit "failed to configure $IPADDR/$NETMASKBITS on $interface."
    fi

    if [ -n "$router" ] ; then
	while ip route del dev $interface 2>/dev/null ; do
	    :
	done
	
	for ii in $router ; do
	    ip route add default via $ii dev $interface
	done
    fi
    
    rm $RESOLV_CONF
    [ -n "$domain" ] && echo domain $domain >> $RESOLV_CONF
    for ii in $dns ; do
	echo nameserver $ii >> $RESOLV_CONF
    done
    if pidof dnsmasq >/dev/null ; then
	kill -HUP `pidof dnsmasq`  # triggers re-read of resolv.conf
    fi

    rm $NTPSERVER_CONF
    for ii in $ntpsrv ; do
	echo NTPSERVER=$ii >> $NTPSERVER_CONF
    done

    /etc/init.d/firewall restart
    /etc/init.d/l2tpd restart
    /usr/bin/noip2 -c /boot/noip2.conf -i $IPADDR 2>/dev/null >/dev/null
}

handle_deconfig() {
    ADDR=`ip -oneline -family inet addr show dev eth1 | awk '{ print $4 }'`
    if [ ! -z "$ADDR" ] ; then
	if ! ip address delete $ADDR brd + dev $interface ; then 
	    error_exit "failed to remove $ADDR on $interface."
	fi 
    fi
    echo -n > $RESOLV_CONF
    echo -n > $NTPSERVER_CONF
    /etc/init.d/l2tpd stop
}

case "$1" in
    bound|renew)
        handle_bound ;;
    deconfig)
        handle_deconfig ;;
    nak)
        error_exit $message ;;
esac
# 

Example D.8. /etc/sysconfig/network/udhcpc.script