中国青基会
RSS
热门关键字:  None  linux+moodle安装  mac  rhel5  199
当前位置 : Nixsky>网管技术>路由>列表

电信网通双线网吧策略路由的实现以及抓取电信运营商地址段的脚本

来源: 作者: 时间:2007-09-10 点击:

基本思路是获取电信IP地址列表,通过设置路由策略把访问电信地址的路由指向电信的网关,剩下的默认走网通的网关。下面的脚本仅通过路由策略设置实现基本的流量分割,没有一方断线自动切换的功能。

#!/bin/sh

#ctc电信 cnc网通

#获取ip命令的绝对地址
IP=`which ip`

#定义电信网通的网关和网络接口
ctc_gw="x.x.x.x"
ctc_if="eth1"
cnc_gw="y.y.y.y"
cnc_if="eth2"

#定义电信的路由表名称和路由策略优先级
ctc_rt_id="100"
ctc_rt_pref="100"

ctc_rt_exist=`cat /etc/iproute2/rt_tables|grep "$ctc_rt_id ctc"|wc -l`
if [ $ctc_rt_exist -eq 0 ]; then
        echo "$ctc_rt_id ctc" >> /etc/iproute2/rt_tables
fi

# 设置route table ctc的默认路由
$IP route replace default via $ctc_gw dev $ctc_if table ctc
        
# route table default的默认路由指向cnc_gw
$IP route replace default via $cnc_gw dev $cnc_if table default
        
# 清除当前访问ctc-network的路由策略
$IP rule show |grep "^$ctc_rt_pref:" | while read ignore ignore ignore r1 r2 ignore t; do
        $IP rule del $r1 $r2 table $t
done
        
# 创建访问ctc-network的路由策略指向route table ctc,ctc-network列出所有属于ctc的网段,这个论坛上有个脚本可以自动抓取,附在后面了。
grep ^[0-9] ./ctc-network | while read ctcnet; do
        $IP rule add pref $ctc_rt_pref to $ctcnet table ctc
done
        
$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里保存下来的,忘记是哪位大虾的作品了。

#!/bin/sh
FILE=./ip_apnic
rm -f $FILE
wget http:
//ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -O $FILE

grep 'apnic|CN|ipv4|' $FILE | cut -f 4,5 -d'|'|sed -e 's/|/ /g' | while read ip cnt
do
        echo $ip:$cnt
        mask=$(cat << EOF | bc | tail -1
        pow=32;
        define log2(x) {
        if (x<=1) return (pow);
                pow--;
                return(log2(x/2));
        }
        log2($cnt)
EOF)
        echo $ip/$mask>> cn.net
        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'`
        NETNAME=`echo $NETNAME | sed -e 's/cJ/ /g' | awk -F' ' '{ printf $1; }'`
       case $NETNAME in
       CNC)
               echo $ip/$mask >> CNCGROUP
       ;;
       CHINANET|CNCGROUP)
               echo $ip/$mask >> $NETNAME
       ;;
       CHINATELECOM)
               echo $ip/$mask >> CHINANET
       ;;
       *)
               echo $ip/$mask >> OTHER
       ;;
       esac
done

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册