2011年8月29日 星期一

[Network] Configuring multiple default routes and name resolving with 1 network interface card

上篇的多重routing與name resolving,再稍做改良就可以用一張網卡同時使用PPPoE撥號連線並支援多重路由,直接看script吧
#!/bin/bash

# pppoe dial up
RETRY=5
PPP=`ifconfig|grep -in ppp`
until [ $RETRY -le 0 ] || [ -n "$PPP" ]
do pon dsl-provider
  sleep 30
  PPP=`ifconfig|grep -in ppp`
  ((RETRY=RETRY-1))
done

# eth0 setting
IF2=eth0
IP2=10.157.132.104
NM2=255.255.240.0
NT2=10.157.128.0/20
GW2=10.157.143.254

IP3=172.17.125.5
NM3=255.255.255.0
NT3=172.17.125.0/24

# some network for my own purpose
NM4=224.0.0.0

# default gateway
DGW=${GW2}

# set ip/netmask
ifconfig ${IF2} ${IP2} netmask ${NM2}

# create a new policy routing table entry
T2=`cat /etc/iproute2/rt_tables|awk '{print $2}'|grep -in T2|cut -d : -f 2`
if [ -z "$T2" ]; then
  echo "1 T2" >> /etc/iproute2/rt_tables
fi

# add new entry within this policy table
ip route add ${NT2} dev ${IF2} src ${IP2} table T2
ip route add default via ${GW2} dev ${IF2} table T2

ip rule add from ${NT2} table T2
ip rule add to ${NT2} table T2

ip rule add from ${NT3} table T2
ip rule add to ${NT3} table T2

# add some routing for my own purpose
route add -net ${NM4} netmask ${NM4} dev ${IF2}

# options timeout:1 rotate attempts:1
echo "options rotate"          >  /etc/resolv.conf
echo "nameserver 168.95.192.1" >> /etc/resolv.conf
echo "nameserver 172.17.125.5" >> /etc/resolv.conf

# disable reverse packet filter for my own purpose
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/eth0/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/lo/rp_filter

沒有留言: