simple.script 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "Setting IP address 0.0.0.0 on $interface"
  16. if command -v ip >/dev/null; then
  17. ip addr flush dev $interface
  18. else
  19. ifconfig $interface 0.0.0.0
  20. fi
  21. ;;
  22. renew|bound)
  23. echo "Setting IP address $ip on $interface"
  24. if command -v ip >/dev/null; then
  25. ip addr add $ip$NETMASK $BROADCAST dev $interface
  26. else
  27. ifconfig $interface $ip $NETMASK $BROADCAST
  28. fi
  29. if [ -n "$router" ] ; then
  30. echo "Deleting routers"
  31. while route del default gw 0.0.0.0 dev $interface ; do
  32. :
  33. done
  34. metric=0
  35. for i in $router ; do
  36. echo "Adding router $i"
  37. if [ "$subnet" = "255.255.255.255" ]; then
  38. # special case for /32 subnets:
  39. # /32 instructs kernel to always use routing for all outgoing packets
  40. # (they can never be sent to local subnet - there is no local subnet for /32).
  41. # Used in datacenters, avoids the need for private ip-addresses between two hops.
  42. ip route add $i dev $interface
  43. fi
  44. route add default gw $i dev $interface metric $((metric++))
  45. done
  46. fi
  47. echo "Recreating $RESOLV_CONF"
  48. # If the file is a symlink somewhere (like /etc/resolv.conf
  49. # pointing to /run/resolv.conf), make sure things work.
  50. realconf=$(readlink -f "$RESOLV_CONF" 2>/dev/null || echo "$RESOLV_CONF")
  51. tmpfile="$realconf-$$"
  52. > "$tmpfile"
  53. [ -n "$domain" ] && echo "search $domain" >> "$tmpfile"
  54. for i in $dns ; do
  55. echo " Adding DNS server $i"
  56. echo "nameserver $i" >> "$tmpfile"
  57. done
  58. mv "$tmpfile" "$realconf"
  59. ;;
  60. esac
  61. exit 0