cut: code shrink
[busybox-git.git] / examples / udhcp / simple.script
blobf079ec8b849fed1414adc70f49f971ce897c15ff
1 #!/bin/sh
2 # udhcpc script edited by Tim Riker <Tim@Rikers.org>
4 RESOLV_CONF="/etc/resolv.conf"
6 [ -n "$1" ] || { echo "Error: should be called from udhcpc"; exit 1; }
8 NETMASK=""
9 if command -v ip >/dev/null; then
10 [ -n "$subnet" ] && NETMASK="/$subnet"
11 else
12 [ -n "$subnet" ] && NETMASK="netmask $subnet"
14 BROADCAST="broadcast +"
15 [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
17 case "$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
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
36 if [ -n "$router" ] ; then
37 echo "Deleting routers"
38 while route del default gw 0.0.0.0 dev $interface ; do
40 done
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
52 route add default gw $i dev $interface metric $((metric++))
53 done
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"
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"
73 esac
75 exit 0