busybox/examples/udhcp/simple.script
<<
>>
Prefs
   1#!/bin/sh
   2# udhcpc script edited by Tim Riker <Tim@Rikers.org>
   3
   4RESOLV_CONF="/etc/resolv.conf"
   5
   6[ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }
   7
   8NETMASK=""
   9if command -v ip >/dev/null; then
  10        [ -n "$subnet" ] && NETMASK="/$subnet"
  11else
  12        [ -n "$subnet" ] && NETMASK="netmask $subnet"
  13fi
  14BROADCAST="broadcast +"
  15[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
  16
  17case "$1" in
  18        deconfig)
  19                echo "Clearing IP addresses on $interface, upping it"
  20                if command -v ip >/dev/null; then
  21                        ip -4 addr flush dev $interface
  22                        ip link set dev $interface up
  23                else
  24                        ifconfig $interface 0.0.0.0
  25                fi
  26                ;;
  27
  28        renew|bound)
  29                echo "Setting IP address $ip on $interface"
  30                if command -v ip >/dev/null; then
  31                        ip addr add $ip$NETMASK $BROADCAST dev $interface
  32                else
  33                        ifconfig $interface $ip $NETMASK $BROADCAST
  34                fi
  35
  36                if [ -n "$router" ] ; then
  37                        echo "Deleting routers"
  38                        while route del default gw 0.0.0.0 dev $interface ; do
  39                                :
  40                        done
  41
  42                        metric=0
  43                        for i in $router ; do
  44                                echo "Adding router $i"
  45                                if [ "$subnet" = "255.255.255.255" ]; then
  46        # special case for /32 subnets:
  47        # /32 instructs kernel to always use routing for all outgoing packets
  48        # (they can never be sent to local subnet - there is no local subnet for /32).
  49        # Used in datacenters, avoids the need for private ip-addresses between two hops.
  50                                        ip route add $i dev $interface
  51                                fi
  52                                route add default gw $i dev $interface metric $((metric++))
  53                        done
  54                fi
  55
  56                # If the file is a symlink somewhere (like /etc/resolv.conf
  57                # pointing to /run/resolv.conf), make sure things work.
  58                if test -L "$RESOLV_CONF"; then
  59                        # If it's a dangling symlink, try to create the target.
  60                        test -e "$RESOLV_CONF" || touch "$RESOLV_CONF"
  61                fi
  62                realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
  63                echo "Recreating $realconf"
  64                tmpfile="$realconf-$$"
  65                > "$tmpfile"
  66                [ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
  67                for i in $dns ; do
  68                        echo " Adding DNS server $i"
  69                        echo "nameserver $i" >> "$tmpfile"
  70                done
  71                mv "$tmpfile" "$realconf"
  72                ;;
  73esac
  74
  75exit 0
  76