File: staticroutes

package info (click to toggle)
ifupdown-scripts-zg2 0.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 484 kB
  • ctags: 55
  • sloc: sh: 4,037; makefile: 37
file content (101 lines) | stat: -rwxr-xr-x 3,353 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
# $Header: /var/local/cvs/debian/ifupdown-scripts-zg2/scripts/ifupdown-scripts-zg2.d/staticroutes,v 1.1 2004/09/12 18:00:04 mh Exp $

# 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