/network/ifupdown-scripts-zg2.d/staticroutes

http://github.com/brinkman83/bashrc · #! · 100 lines · 88 code · 12 blank · 0 comment · 0 complexity · 90ae5f3643d344ed2d584ffbee598cc1 MD5 · raw file

  1. #!/bin/bash
  2. # IFACE = Logical interface name
  3. # MODE = start | stop
  4. # METHOD = manual, otherwise exit!
  5. # IF_ROUTEn = static routes (network/prefix gateway)
  6. # IF_DEVICE = device to be used
  7. . /etc/network/ifupdown-scripts-zg2.d/common-functions
  8. case "$MODE" in
  9. start)
  10. # adds static routes given in config file
  11. for R in ${!IF_ROUTE*}; do
  12. eval S=\$$R
  13. verbose "route entry $S"
  14. # a "route_foo" statement in the config file can use two different
  15. # forms of syntax:
  16. # route_foo prefix gateway/device metric/table
  17. # route_foo ip route <cmd line> and
  18. # if the string after "route_foo" in the config file does not start
  19. # with "ip route", the following words are interpreted as
  20. # prefix - prefix for the route, used as "to" parameter.
  21. # gateway/device - if IP address, gateway for the route,
  22. # used as "via" parameter,
  23. # otherwise, device for the route,
  24. # used as "dev" parameter.
  25. # metric/table - if numeric, metric for the route,
  26. # used as "metric" parameter.
  27. # otherwise, table for the route,
  28. # used as "route" parameter.
  29. # From these values, appropriate "ip route" command lines are
  30. # constructed for interface startup and shutdown. This should cover
  31. # almost all non-exotic cases.
  32. # if the string after "route_foo" in the config file starts with
  33. # "ip route", the remainder of the string is taken as a full ip route
  34. # command line which will be appended to "ip route add" on interface
  35. # startup and to "ip route del" on interface shutdown.
  36. ROUTEPARM=""
  37. if [[ "$S" = "ip route"* ]]; then
  38. ROUTEPARM="${S#ip route }"
  39. verbose "explicit ip command ip route $ROUTEPARM"
  40. else
  41. # parse prefix gateway/device metric/table to variables and build
  42. # ROUTEPARM
  43. # take each word in $S and prefix it with the first word of PARAMS.
  44. # then cut each word from PARAMS.
  45. # concatenate each of the resulting strings to a valid ip command line.
  46. PARAMS="to gatewaydevice metrictable END"
  47. for word in $S; do
  48. if [ "$PARAMS" = "END" ]; then
  49. abort "too many parameters in static route $S"
  50. fi
  51. case "${PARAMS%% *}" in
  52. gatewaydevice)
  53. if echo $word | grep --quiet '^[0-9a-f:\.]*$'; then
  54. ROUTEPARM="$ROUTEPARM via $word"
  55. else
  56. ROUTEPARM="$ROUTEPARM dev $word"
  57. fi
  58. ;;
  59. metrictable)
  60. if echo $word | grep --quiet '^[0-9]\+$'; then
  61. ROUTEPARM="$ROUTEPARM metric $word"
  62. else
  63. ROUTEPARM="$ROUTEPARM table $word"
  64. fi
  65. ;;
  66. *)
  67. ROUTEPARM="$ROUTEPARM ${PARAMS%% *} $word"
  68. ;;
  69. esac
  70. PARAMS="${PARAMS#* }"
  71. done
  72. if [ "${PARAMS%% *}" = "dest" ]; then
  73. # no destination given, take interface instead
  74. ROUTEPARM="$ROUTEPARM dev $IF_DEVICE"
  75. fi
  76. verbose "constructed ip command ip route $ROUTEPARM"
  77. fi
  78. cmd "ip route add $ROUTEPARM"
  79. add_down "routes" "route del $ROUTEPARM"
  80. done
  81. ;;
  82. stop)
  83. exec_down "routes" "ip"
  84. ;;
  85. *)
  86. ;;
  87. esac
  88. # end of file