From 8c804d83125c275fd3c5ef8e32b002e2f785a6b3 Mon Sep 17 00:00:00 2001 From: mitty Date: Mon, 29 Jun 2009 08:56:34 +0000 Subject: [PATCH] * setroute.sh * rewrite codes with environment variables given by INTERFACES(5) * setroute-rhel.sh * RHEL capable version git-svn-id: https://lab.mitty.jp/svn/lab/trunk@6 7d2118f6-f56c-43e7-95a2-4bb3031d96e7 --- iproute/setroute-rhel.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ iproute/setroute.sh | 29 ++++++++++++++++++----------- 2 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 iproute/setroute-rhel.sh diff --git a/iproute/setroute-rhel.sh b/iproute/setroute-rhel.sh new file mode 100644 index 0000000..e88bdc6 --- /dev/null +++ b/iproute/setroute-rhel.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}" diff --git a/iproute/setroute.sh b/iproute/setroute.sh index e88bdc6..f493901 100644 --- a/iproute/setroute.sh +++ b/iproute/setroute.sh @@ -2,29 +2,36 @@ set -x -IF="${1:?"usage: $0 [gateway] "}" -GATEWAY=$2 +## INTERFACES(5) +# IFACE physical name of the interface being processed +# METHOD method of the interface (e.g., static) +# MODE start if run from ifup, stop if run from ifdown +# PHASE as per MODE, but with finer granularity, distinguishing the pre- +# up, post-up, pre-down and post-down phases. -TID=`/sbin/ip addr show dev ${IF} | grep -w ${IF}: | cut -d : -f 1` +# optional arugument to set default gateway manually +GATEWAY=$1 -if [ "${GATEWAY}" = "down" ]; then +TID=`/sbin/ip addr show dev ${IFACE} | grep -w ${IFACE}: | cut -d : -f 1` + +if [ "${PHASE}" = "post-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}" + echo "remove routing table and policy for ${IFACE}" 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` +IPADDR=`/sbin/ip addr show dev ${IFACE} | grep -w inet | cut -d t -f 2 | cut -d ' ' -f 2 | cut -d / -f 1` +NETWORK=`/sbin/ip route show dev ${IFACE} | 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` + GATEWAY=`/sbin/ip route show dev ${IFACE} | grep -w default | cut -d ' ' -f 3` fi if [ -z "${GATEWAY}" ]; then GATEWAY=`/sbin/ip route | grep -w default | cut -d ' ' -f 3` @@ -33,11 +40,11 @@ fi # set routing table -/sbin/ip route add ${NETWORK} dev ${IF} table ${TID} -/sbin/ip route add default via ${GATEWAY} dev ${IF} table ${TID} +/sbin/ip route add ${NETWORK} dev ${IFACE} table ${TID} +/sbin/ip route add default via ${GATEWAY} dev ${IFACE} 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}" +echo "add routing table and policy for ${IFACE} on table ${TID}" -- 1.7.9.5