[8c804d8] | 1 | #!/bin/sh |
---|
| 2 | |
---|
| 3 | set -x |
---|
| 4 | |
---|
[84d8293] | 5 | IFACE="${1:?"usage: $0 <ether_device> [gateway] "}" |
---|
[8c804d8] | 6 | GATEWAY=$2 |
---|
| 7 | |
---|
[84d8293] | 8 | TID=`/sbin/ip addr show dev ${IFACE} | grep -w ${IFACE}: | tr -s ' ' | cut -d : -f 1` |
---|
[8c804d8] | 9 | |
---|
| 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 |
---|
[84d8293] | 17 | echo "remove routing table and policy for ${IFACE}" |
---|
[8c804d8] | 18 | exit |
---|
| 19 | fi |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | # get IP address, network mask, default gateway info |
---|
[84d8293] | 23 | IPADDR=`/sbin/ip addr show dev ${IFACE} | grep -w inet | tr -s ' ' | cut -d ' ' -f 3 | cut -d / -f 1` |
---|
| 24 | NETWORK=`/sbin/ip route show dev ${IFACE} | grep -w ${IPADDR} | tr -s ' ' | cut -d ' ' -f 1` |
---|
[8c804d8] | 25 | |
---|
| 26 | if [ -z "${GATEWAY}" ]; then |
---|
[84d8293] | 27 | GATEWAY=`/sbin/ip route show dev ${IFACE} | grep -w default | tr -s ' ' | cut -d ' ' -f 3` |
---|
[8c804d8] | 28 | fi |
---|
| 29 | if [ -z "${GATEWAY}" ]; then |
---|
[84d8293] | 30 | GATEWAY=`/sbin/ip route | grep -w default | tr -s ' ' | cut -d ' ' -f 3` |
---|
[8c804d8] | 31 | # this may add wrong default route especially on RHEL |
---|
| 32 | fi |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | # set routing table |
---|
[84d8293] | 36 | /sbin/ip route add ${NETWORK} dev ${IFACE} table ${TID} |
---|
| 37 | /sbin/ip route add default via ${GATEWAY} dev ${IFACE} table ${TID} |
---|
[8c804d8] | 38 | |
---|
| 39 | # set routing policy |
---|
| 40 | /sbin/ip rule add from ${IPADDR} table ${TID} priority ${TID} |
---|
| 41 | |
---|
| 42 | /sbin/ip route flush cache |
---|
[84d8293] | 43 | echo "add routing table and policy for ${IFACE} on table ${TID}" |
---|