source: lab.git/TipAndDoc/iproute/setroute.sh @ f05e973

trunk
Last change on this file since f05e973 was 8333ea0, checked in by mitty <mitty@…>, 14 years ago

git-svn-id: https://lab.mitty.jp/svn/lab/trunk@92 7d2118f6-f56c-43e7-95a2-4bb3031d96e7

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