From: mitty Date: Sun, 28 Jun 2009 16:59:49 +0000 (+0000) Subject: * automatically set route tables for each ether device with iproute2 X-Git-Tag: r89-trunk~73 X-Git-Url: http://lab.mitty.jp/git/?a=commitdiff_plain;h=be2a37ed8f486b21416d8cd1dc1fbae3dea47c33;p=lab.git * automatically set route tables for each ether device with iproute2 * this script should be used with /etc/network/interfaces on Ubuntu(Debian) * it maybe work on RHEL(CentOS) with some restrictions git-svn-id: https://lab.mitty.jp/svn/lab/trunk@5 7d2118f6-f56c-43e7-95a2-4bb3031d96e7 --- diff --git a/iproute/setroute.sh b/iproute/setroute.sh new file mode 100644 index 0000000..e88bdc6 --- /dev/null +++ b/iproute/setroute.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +set -x + +IF="${1:?"usage: $0 [gateway] "}" +GATEWAY=$2 + +TID=`/sbin/ip addr show dev ${IF} | grep -w ${IF}: | cut -d : -f 1` + +if [ "${GATEWAY}" = "down" ]; then + # delete routing table and policy and exit + ## routing table is automatically flushed, so 'route flush' can be removed + /sbin/ip route flush table ${TID} + ## device has no longer any IP addr, so do not use IPADDR + /sbin/ip rule del table ${TID} priority ${TID} + /sbin/ip route flush cache + echo "remove routing table and policy for ${IF}" + exit +fi + + +# get IP address, network mask, default gateway info +IPADDR=`/sbin/ip addr show dev ${IF} | grep -w inet | cut -d t -f 2 | cut -d ' ' -f 2 | cut -d / -f 1` +NETWORK=`/sbin/ip route show dev ${IF} | grep -w ${IPADDR} | cut -d ' ' -f 1` + +if [ -z "${GATEWAY}" ]; then + GATEWAY=`/sbin/ip route show dev ${IF} | grep -w default | cut -d ' ' -f 3` +fi +if [ -z "${GATEWAY}" ]; then + GATEWAY=`/sbin/ip route | grep -w default | cut -d ' ' -f 3` + # this may add wrong default route especially on RHEL +fi + + +# set routing table +/sbin/ip route add ${NETWORK} dev ${IF} table ${TID} +/sbin/ip route add default via ${GATEWAY} dev ${IF} table ${TID} + +# set routing policy +/sbin/ip rule add from ${IPADDR} table ${TID} priority ${TID} + +/sbin/ip route flush cache +echo "add routing table and policy for ${IF} on table ${TID}"