#!/bin/bash
# clear old config
sed -i -r '/^ip (route|rule).*/d' /etc/rc.local
sed -i '/\/proc\/sys\/net\/ipv4\/conf/d' /etc/rc.local
# add default rp_filter
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter
# interface list
DIR="/etc/sysconfig/network-scripts"
VALUE=200
for iface in `ls $DIR | grep -E -v "^ifcfg-lo" | grep ifcfg`;
do
name=`cat $DIR/$iface | grep -E "DEVICE" | awk -F= '{print $2}'`
ip=`cat $DIR/$iface | grep -E "IPADDR" | awk -F= '{print $2}'`
gw=`cat $DIR/$iface | grep -E "GATEWAY" | awk -F= '{print $2}'`
echo 0 > /proc/sys/net/ipv4/conf/$name/rp_filter
sed -i -r "/.*$name.*/d" /etc/sysctl.conf
cat >>/etc/sysctl.conf <<EOF
net.ipv4.conf.$name.rp_filter=0
EOF
cat >>/etc/rc.local <<EOF
echo 0 > /proc/sys/net/ipv4/conf/$name/rp_filter
EOF
[ $ip ] || continue
[ $gw ] || continue
sed -i -r "/.*$name.*/d" /etc/iproute2/rt_tables
echo "$((VALUE--)) $name" >> /etc/iproute2/rt_tables
cat >>/etc/rc.local <<EOF
ip route flush table $name
ip route add default via $gw dev $name src $ip table $name
ip rule add from $ip table $name

EOF

done
chmod +x /etc/rc.local
. /etc/rc.local
echo "Config Done"