5 IF="${1:?"usage: $0 <ether_device> [gateway] "}"
8 TID=`/sbin/ip addr show dev ${IF} | grep -w ${IF}: | cut -d : -f 1`
10 if [ "${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
17 echo "remove routing table and policy for ${IF}"
22 # get IP address, network mask, default gateway info
23 IPADDR=`/sbin/ip addr show dev ${IF} | grep -w inet | cut -d t -f 2 | cut -d ' ' -f 2 | cut -d / -f 1`
24 NETWORK=`/sbin/ip route show dev ${IF} | grep -w ${IPADDR} | cut -d ' ' -f 1`
26 if [ -z "${GATEWAY}" ]; then
27 GATEWAY=`/sbin/ip route show dev ${IF} | grep -w default | cut -d ' ' -f 3`
29 if [ -z "${GATEWAY}" ]; then
30 GATEWAY=`/sbin/ip route | grep -w default | cut -d ' ' -f 3`
31 # this may add wrong default route especially on RHEL
36 /sbin/ip route add ${NETWORK} dev ${IF} table ${TID}
37 /sbin/ip route add default via ${GATEWAY} dev ${IF} table ${TID}
40 /sbin/ip rule add from ${IPADDR} table ${TID} priority ${TID}
42 /sbin/ip route flush cache
43 echo "add routing table and policy for ${IF} on table ${TID}"