source: lab.git/iproute/setroute.sh @ 84c2881

trunk
Last change on this file since 84c2881 was 84c2881, checked in by mitty <mitty@…>, 15 years ago
  • abort if fail to bind IP address to NIC
    • no DHCPd, disconnecting cables, etc...

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

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[be2a37e]1#!/bin/sh
2
3set -x
4
[8c804d8]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.
[be2a37e]11
[8c804d8]12# optional arugument to set default gateway manually
13GATEWAY=$1
[be2a37e]14
[8c804d8]15TID=`/sbin/ip addr show dev ${IFACE} | grep -w ${IFACE}: | cut -d : -f 1`
16
17if [ "${PHASE}" = "post-down" ]; then
[be2a37e]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
[8c804d8]24    echo "remove routing table and policy for ${IFACE}"
[be2a37e]25    exit
26fi
27
28
29# get IP address, network mask, default gateway info
[8c804d8]30IPADDR=`/sbin/ip addr show dev ${IFACE} | grep -w inet | cut -d t -f 2 | cut -d ' ' -f 2 | cut -d / -f 1`
[84c2881]31if [ -z ${IPADDR} ]; then
32    # fail to bind IP address to device
33    echo "${IFACE} has no IP address."
34    exit
35fi
[8c804d8]36NETWORK=`/sbin/ip route show dev ${IFACE} | grep -w ${IPADDR} | cut -d ' ' -f 1`
[be2a37e]37
38if [ -z "${GATEWAY}" ]; then
[8c804d8]39    GATEWAY=`/sbin/ip route show dev ${IFACE} | grep -w default | cut -d ' ' -f 3`
[be2a37e]40fi
41if [ -z "${GATEWAY}" ]; then
42    GATEWAY=`/sbin/ip route | grep -w default | cut -d ' ' -f 3`
43    # this may add wrong default route especially on RHEL
44fi
45
46
47# set routing table
[8c804d8]48/sbin/ip route add ${NETWORK} dev ${IFACE} table ${TID}
49/sbin/ip route add default via ${GATEWAY} dev ${IFACE} table ${TID}
[be2a37e]50
51# set routing policy
52/sbin/ip rule add from ${IPADDR} table ${TID} priority ${TID}
53
54/sbin/ip route flush cache
[8c804d8]55echo "add routing table and policy for ${IFACE} on table ${TID}"
Note: See TracBrowser for help on using the repository browser.