File: static-routes

package info (click to toggle)
ifupdown-extra 0.12
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 88 kB
  • ctags: 25
  • sloc: sh: 514; makefile: 50
file content (76 lines) | stat: -rwxr-xr-x 3,052 bytes parent folder | download
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
#!/bin/sh
#
# Script to setup a system's static routes based on the
# /etc/network/routes definitions.
# It tries to simplify network route management and make it easier
# to handle those as requested in bug #368228 ('Wish: Better Handling 
# of up/down route commands'). With this script routes do not have to be 
# introduced in /etc/network/interfaces (in 'up' and 'down' commands).
#
# This file includes a list of routes for different networks and follows
# the format:
# Network Netmask Gateway Interface
# 
# Example:
# 172.1.1.0 255.255.255.0 192.168.0.1 eth0
#
# Install this script in /etc/network/if-up.d/ (to setup the routes) and in 
# /etc/network/if-pre-down.d/ (if you want to remove the routes before
# deconfiguring the interface)
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#  
# You can also find a copy of the GNU General Public License at
# http://www.gnu.org/licenses/licenses.html#TOCLGPL
#
#
# TODO:
#  - No action is taken if the routes already exist (when adding them) or when
#    they dont (if you are removing), if they do you will get an error in
#    stderr but the script will continue 
#    Note: If you add the up/down in /etc/network/interfaces failure when
#          setting up a route breaks the interface configuration

# Abort if the configuration file does not exist
[ ! -r /etc/network/routes ] && exit 0
[ -z "$VERBOSITY" ] && VERBOSITY = 0

del_static_routes() {
	# NOTE: We actually don't have to remove routes if downing an interface
	# since they will be removed nevertheless. In any case, this 
	# piece of code only runs if you install this file in 
	# /etc/network/if-pre-down.d/ (which you don't need to)
	cat /etc/network/routes | egrep "${IFACE}$" | 
	while read network netmask gateway interface ; do
		[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Deleting route for $network / $netmask through gateway $gateway at $interface"
		route del -net $network gw $gateway netmask $netmask dev $interface
	done
}

add_static_routes() {
	cat /etc/network/routes | egrep "${IFACE}$" | 
	while read network netmask gateway interface ; do
		[ "$VERBOSITY" -eq 1 ] && echo "DEBUG: Adding route for $network / $netmask through gateway $gateway at $interface"
		route add -net $network netmask $netmask gw $gateway dev $interface
	done
}

case "$MODE" in 
	start) add_static_routes ;;
	stop)  del_static_routes ;;
	*)     ;;
esac

exit 0