/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
- #!/bin/bash
- # IFACE = Logical interface name
- # MODE = start | stop
- # METHOD = manual, otherwise exit!
- # IF_ROUTEn = static routes (network/prefix gateway)
- # IF_DEVICE = device to be used
- . /etc/network/ifupdown-scripts-zg2.d/common-functions
- case "$MODE" in
- start)
- # adds static routes given in config file
- for R in ${!IF_ROUTE*}; do
- eval S=\$$R
- verbose "route entry $S"
- # a "route_foo" statement in the config file can use two different
- # forms of syntax:
-
- # route_foo prefix gateway/device metric/table
- # route_foo ip route <cmd line> and
-
- # if the string after "route_foo" in the config file does not start
- # with "ip route", the following words are interpreted as
- # prefix - prefix for the route, used as "to" parameter.
- # gateway/device - if IP address, gateway for the route,
- # used as "via" parameter,
- # otherwise, device for the route,
- # used as "dev" parameter.
- # metric/table - if numeric, metric for the route,
- # used as "metric" parameter.
- # otherwise, table for the route,
- # used as "route" parameter.
- # From these values, appropriate "ip route" command lines are
- # constructed for interface startup and shutdown. This should cover
- # almost all non-exotic cases.
- # if the string after "route_foo" in the config file starts with
- # "ip route", the remainder of the string is taken as a full ip route
- # command line which will be appended to "ip route add" on interface
- # startup and to "ip route del" on interface shutdown.
- ROUTEPARM=""
- if [[ "$S" = "ip route"* ]]; then
- ROUTEPARM="${S#ip route }"
- verbose "explicit ip command ip route $ROUTEPARM"
- else
- # parse prefix gateway/device metric/table to variables and build
- # ROUTEPARM
-
- # take each word in $S and prefix it with the first word of PARAMS.
- # then cut each word from PARAMS.
- # concatenate each of the resulting strings to a valid ip command line.
-
- PARAMS="to gatewaydevice metrictable END"
- for word in $S; do
- if [ "$PARAMS" = "END" ]; then
- abort "too many parameters in static route $S"
- fi
- case "${PARAMS%% *}" in
- gatewaydevice)
- if echo $word | grep --quiet '^[0-9a-f:\.]*$'; then
- ROUTEPARM="$ROUTEPARM via $word"
- else
- ROUTEPARM="$ROUTEPARM dev $word"
- fi
- ;;
- metrictable)
- if echo $word | grep --quiet '^[0-9]\+$'; then
- ROUTEPARM="$ROUTEPARM metric $word"
- else
- ROUTEPARM="$ROUTEPARM table $word"
- fi
- ;;
- *)
- ROUTEPARM="$ROUTEPARM ${PARAMS%% *} $word"
- ;;
- esac
- PARAMS="${PARAMS#* }"
- done
- if [ "${PARAMS%% *}" = "dest" ]; then
- # no destination given, take interface instead
- ROUTEPARM="$ROUTEPARM dev $IF_DEVICE"
- fi
- verbose "constructed ip command ip route $ROUTEPARM"
- fi
- cmd "ip route add $ROUTEPARM"
- add_down "routes" "route del $ROUTEPARM"
- done
- ;;
- stop)
- exec_down "routes" "ip"
- ;;
- *)
- ;;
- esac
- # end of file