作者: 胖头鱼  
基本思路是获取电信IP地址列表,通过设置路由策略把访问电信地址的路由指向电信的网关,剩下的默认走网通的网关。下面的脚本仅通过路由策略设置实现基本的流量分割,没有一方断线自动切换的功能。
  1. #!/bin/sh   
  2.    
  3. #ctc电信 cnc网通   
  4.    
  5. #获取ip命令的绝对地址   
  6. IP=`which ip`   
  7.    
  8. #定义电信网通的网关和网络接口   
  9. ctc_gw="x.x.x.x"   
  10. ctc_if="eth1"   
  11. cnc_gw="y.y.y.y"   
  12. cnc_if="eth2"   
  13.    
  14. #定义电信的路由表名称和路由策略优先级   
  15. ctc_rt_id="100"   
  16. ctc_rt_pref="100"   
  17.    
  18. ctc_rt_exist=`cat /etc/iproute2/rt_tables|grep "$ctc_rt_id ctc"|wc -l`   
  19. if [ $ctc_rt_exist -eq 0 ]; then   
  20.         echo "$ctc_rt_id ctc" >> /etc/iproute2/rt_tables   
  21. fi   
  22.    
  23. # 设置route table ctc的默认路由   
  24. $IP route replace default via $ctc_gw dev $ctc_if table ctc   
  25.    
  26. # route table default的默认路由指向cnc_gw   
  27. $IP route replace default via $cnc_gw dev $cnc_if table default   
  28.    
  29. # 清除当前访问ctc-network的路由策略   
  30. $IP rule show |grep "^$ctc_rt_pref:" | while read ignore ignore ignore r1 r2 ignore t; do   
  31.         $IP rule del $r1 $r2 table $t   
  32. done   
  33.    
  34. # 创建访问ctc-network的路由策略指向route table ctc,ctc-network列出所有属于ctc的网段,chinaunix.net论坛上有个脚本可以自动抓取,附在后面了。   
  35. grep ^[0-9] ./ctc-network | while read ctcnet; do   
  36.         $IP rule add pref $ctc_rt_pref to $ctcnet table ctc   
  37. done   
  38.    
  39. $IP route flush cache  
复制代码
流量分隔实现后,NAT规则这样写:
local_net="局域网地址"
cnc_if="网通接口网卡"
cnc_ip="网通接口网卡IP"
ctc_if="电信接口网卡"
cnc_ip="电信接口网卡IP"

iptables -t nat -A POSTROUTING -s $local_net -o $cnc_if -j SNAT --to $cnc_ip
iptables -t nat -A POSTROUTING -s $local_net -o $ctc_if -j SNAT --to $ctc_ip

下面的脚本可以抓取各运营商的IP地址网段,从CU里保存下来的,忘记是哪位大虾的作品了
  1. #!/bin/sh   
  2. FILE=./ip_apnic   
  3. rm -f $FILE   
  4. wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -O $FILE   
  5.    
  6. grep 'apnic|CN|ipv4|' $FILE | cut -f 4,5 -d'|'|sed -e 's/|/ /g' | while read ip cnt   
  7. do   
  8.         echo $ip:$cnt   
  9.         mask=$(cat << EOF | bc | tail -1   
  10.         pow=32;   
  11.         define log2(x) {   
  12.         if (x<=1) return (pow);   
  13.                 pow--;   
  14.                 return(log2(x/2));   
  15.         }   
  16.         log2($cnt)   
  17. EOF)   
  18.         echo $ip/$mask>> cn.net   
  19.         NETNAME=`whois $ip@whois.apnic.net | sed -e '/./{H;$!d;}' -e 'x;/netnum/!d' |grep ^netname | sed -e 's/.*: \(.*\)/\1/g' | sed -e 's/-.*//g'`   
  20.         NETNAME=`echo $NETNAME | sed -e 's/cJ/ /g' | awk -F' ' '{ printf $1; }'`   
  21.        case $NETNAME in   
  22.        CNC)   
  23.                echo $ip/$mask >> CNCGROUP   
  24.        ;;   
  25.        CHINANET|CNCGROUP)   
  26.                echo $ip/$mask >> $NETNAME   
  27.        ;;   
  28.        CHINATELECOM)   
  29.                echo $ip/$mask >> CHINANET   
  30.        ;;   
  31.        *)   
  32.                echo $ip/$mask >> OTHER   
  33.        ;;   
  34.        esac   
  35. done   
复制代码