PageRenderTime 130ms CodeModel.GetById 124ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

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