source: lab/trunk/iproute/setroute.sh @ 6

Last change on this file since 6 was 6, checked in by mitty, 15 years ago
  • setroute.sh
    • rewrite codes with environment variables given by INTERFACES(5)
  • setroute-rhel.sh
    • RHEL capable version
File size: 1.7 KB
Line 
1#!/bin/sh
2
3set -x
4
5## INTERFACES(5)
6# IFACE  physical name of the interface being processed
7# METHOD method of the interface (e.g., static)
8# MODE   start if run from ifup, stop if run from ifdown
9# PHASE  as per MODE, but with finer granularity, distinguishing the pre-
10#        up, post-up, pre-down and post-down phases.
11
12# optional arugument to set default gateway manually
13GATEWAY=$1
14
15TID=`/sbin/ip addr show dev ${IFACE} | grep -w ${IFACE}: | cut -d : -f 1`
16
17if [ "${PHASE}" = "post-down" ]; then
18    # delete routing table and policy and exit
19    ## routing table is automatically flushed, so 'route flush' can be removed
20    /sbin/ip route flush table ${TID}
21    ## device has no longer any IP addr, so do not use IPADDR
22    /sbin/ip rule del table ${TID} priority ${TID}
23    /sbin/ip route flush cache
24    echo "remove routing table and policy for ${IFACE}"
25    exit
26fi
27
28
29# get IP address, network mask, default gateway info
30IPADDR=`/sbin/ip addr show dev ${IFACE} | grep -w inet | cut -d t -f 2 | cut -d ' ' -f 2 | cut -d / -f 1`
31NETWORK=`/sbin/ip route show dev ${IFACE} | grep -w ${IPADDR} | cut -d ' ' -f 1`
32
33if [ -z "${GATEWAY}" ]; then
34    GATEWAY=`/sbin/ip route show dev ${IFACE} | grep -w default | cut -d ' ' -f 3`
35fi
36if [ -z "${GATEWAY}" ]; then
37    GATEWAY=`/sbin/ip route | grep -w default | cut -d ' ' -f 3`
38    # this may add wrong default route especially on RHEL
39fi
40
41
42# set routing table
43/sbin/ip route add ${NETWORK} dev ${IFACE} table ${TID}
44/sbin/ip route add default via ${GATEWAY} dev ${IFACE} table ${TID}
45
46# set routing policy
47/sbin/ip rule add from ${IPADDR} table ${TID} priority ${TID}
48
49/sbin/ip route flush cache
50echo "add routing table and policy for ${IFACE} on table ${TID}"
Note: See TracBrowser for help on using the repository browser.