source: lab/trunk/iproute/setroute-rhel.sh @ 14

Last change on this file since 14 was 14, checked in by mitty, 15 years ago
  • FIX: use 'tr' to replace repeated SPACE with a single SPACE before 'cut' lines
  • Property svn:executable set to *
File size: 1.4 KB
RevLine 
[5]1#!/bin/sh
2
3set -x
4
[14]5IFACE="${1:?"usage: $0 <ether_device> [gateway] "}"
[5]6GATEWAY=$2
7
[14]8TID=`/sbin/ip addr show dev ${IFACE} | grep -w ${IFACE}: | tr -s ' ' | cut -d : -f 1`
[5]9
10if [ "${GATEWAY}" = "down" ]; then
11    # delete routing table and policy and exit
12    ## routing table is automatically flushed, so 'route flush' can be removed
13    /sbin/ip route flush table ${TID}
14    ## device has no longer any IP addr, so do not use IPADDR
15    /sbin/ip rule del table ${TID} priority ${TID}
16    /sbin/ip route flush cache
[14]17    echo "remove routing table and policy for ${IFACE}"
[5]18    exit
19fi
20
21
22# get IP address, network mask, default gateway info
[14]23IPADDR=`/sbin/ip addr show dev ${IFACE} | grep -w inet | tr -s ' ' | cut -d ' ' -f 3 | cut -d / -f 1`
24NETWORK=`/sbin/ip route show dev ${IFACE} | grep -w ${IPADDR} | tr -s ' ' | cut -d ' ' -f 1`
[5]25
26if [ -z "${GATEWAY}" ]; then
[14]27    GATEWAY=`/sbin/ip route show dev ${IFACE} | grep -w default | tr -s ' ' | cut -d ' ' -f 3`
[5]28fi
29if [ -z "${GATEWAY}" ]; then
[14]30    GATEWAY=`/sbin/ip route | grep -w default | tr -s ' ' | cut -d ' ' -f 3`
[5]31    # this may add wrong default route especially on RHEL
32fi
33
34
35# set routing table
[14]36/sbin/ip route add ${NETWORK} dev ${IFACE} table ${TID}
37/sbin/ip route add default via ${GATEWAY} dev ${IFACE} table ${TID}
[5]38
39# set routing policy
40/sbin/ip rule add from ${IPADDR} table ${TID} priority ${TID}
41
42/sbin/ip route flush cache
[14]43echo "add routing table and policy for ${IFACE} on table ${TID}"
Note: See TracBrowser for help on using the repository browser.