simple.script 2.0 KB

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