(存档)TTDW路由,单线N拨脚本

#!/bin/ash
# load-balance(X-WAN) Script VER 0.23 by rightzd 20110902 | 单线X WAN版
# 使用方法:
#            双WAN接口模式选择为“单物理口双WAN”, 双WAN模式选择为“自定义脚本”并把本脚本内容粘贴进去,WAN2路由表选择为自定义并保持内容为空
#            iptables -t nat -I POSTROUTING -o ppp+ -j MASQUERADE这句放在系统管理->脚本设置->防火墙脚本里
# 一般只需要修改USER, PASSWD, WANNUM这3个参数就可以
# WAN1, WAN2是固件自动拨号的,从WAN3起由脚本负责拨号。WAN1,WAN2的指定出口功能有效,不过如果修改设置后请重启路由器。

LOGGER=logger
LOCK_FILE=/var/policyroute-set.lock
XWAN_OK=/var/xwan_ok
NETLIST=/var/policyroute-netlist
#宽带用户名和密码
USER="帐号"
PASSWD="密码"
#榨干你的线路,修改这个参数为线路允许的多拨次数
WANNUM=4
MTU=$(nvram get wan_mtu)
MACBASE=$(nvram get wan_hwaddr)

#对MAC进行递增,注意跨度不要太大,目前只支持最后两位的递增
MACINC() {
    mac=$1
    value=$2
    b1=`printf "%d" 0x${mac:0:2}`
    b2=`printf "%d" 0x${mac:3:2}`
    b3=`printf "%d" 0x${mac:6:2}`
    b4=`printf "%d" 0x${mac:9:2}`
    b5=`printf "%d" 0x${mac:12:2}`
    b6=`printf "%d" 0x${mac:15:2}`
    let x=$b6+$value
    let b6=$((x%255))
    let b5=b5+$((x/255))

    let x=b5
    let b5=$((x%255))
    let b4=b4+$((x/255))

    NEWMAC=`printf "%x:%x:%x:%x:%x:%x" ${b1} ${b2} ${b3} ${b4} ${b5} ${b6}`
}

#进行一个PPPoE拨号
PPPOE() {
    iface=$1
    let ipparam=$2-1    
    $(pppoecd $iface -u $USER -p $PASSWD -r $MTU -t $MTU -P $ipparam)
}

#激活一个WAN口,参数类型是数字,如IFUP 3表示激活WAN3
IFUP() {
    parent=$1
    iface="veth"$2
    ip link add link $parent $iface type macvlan
    MACINC $MACBASE $2
    ifconfig $iface hw ether $NEWMAC up
    PPPOE $iface $2
}

IFDOWN() {
    parent=$1
    iface="veth"$2
    ip link del link $parent $iface type macvlan
}

ALLIFUP() {
    wanid=3
    while [ $wanid -le $WANNUM ]
    do
        IFUP vlan1 $wanid
        let wanid=wanid+1
    done
}

ALLIFDOWN() {
    wanid=3
    while [ $wanid -le $WANNUM ]
    do
        IFDOWN vlan1 $wanid
        let wanid=wanid+1
    done
}

GET_PPP_GATEWAY() {
    iface=$1
    echo "iface="$iface
    PPP_IP=$(ifconfig $iface|grep "inet addr"|awk -F ":" '{print $2}'|awk -F " " '{print $1}')
    PPP_GATEWAY=$PPP_IP
}

SET_FIXED_ROUTE() {
    wanid=$1
    rtbl=${wanid}00
    $LOGGER "set fixed route via WAN${wanid}..."
    if [ -f $NETLIST ]; then
        MY_LINE_NO=1
        while read MY_LINE
        do
            ip rule add to $MY_LINE table $rtbl prio 200
            MY_LINE_NO=$((MY_LINE_NO+1))
        done < $NETLIST
    fi
}

BUILD_WAN_ROUTETAB() {
    wanid=$1    
    $LOGGER "set WAN${wanid} route table ..."    
    rtbl=${wanid}00
    if [ "$wanid" == "1" ]; then
        prefix="wan"
    else
        prefix="wan"${wanid}
    fi
    WAN_GATEWAY=$(nvram get ${prefix}_gateway)
    WAN_IFACE=$(nvram get ${prefix}_iface)
    WAN_IP=$(ifconfig $WAN_IFACE|grep "inet addr"|awk -F ":" '{print $2}'|awk -F " " '{print $1}')
    WAN_WEIGHT=$(nvram get xwan_wan${wanid}_weight)
    if [ "$WAN_IP" == "" ]; then
        $LOGGER "Start PolicyRouting fail, BUILD_WAN_ROUTETAB, WAN${wanid} NO IP"
        return
    fi
    
    ip route flush table $rtbl
    ip route | while read ROUTE
    do
      if [ ! "${ROUTE:0:7}" = "default" ]; then
        ip route append table $rtbl to $ROUTE
      fi
    done
    
    ip route replace table $rtbl default via $WAN_GATEWAY dev $WAN_IFACE src $WAN_IP
    
    #设置ip rule
    ip rule add from $WAN_IP table $rtbl prio 100
    
    # DNS Rule
    WAN_GET_DNS=$(nvram get ${prefix}_dns)
    if [ "$WAN_GET_DNS" = "" ]; then
      WAN_GET_DNS=$(nvram get ${prefix}_get_dns)
    fi
    
    for WAN_DNS in $WAN_GET_DNS
    do
        $LOGGER "set WAN${wanid} route($WAN_GATEWAY) for DNS:$WAN_DNS"
        if [ ! "$WAN_DNS" = "0.0.0.0" ]; then
            ip rule add to $WAN_DNS table $rtbl prio 150
        fi
    done
}

SET_ASSIGNOUT_RULE() {
    #8. AssignOUT 规则
    # wan1
    ip rule add fwmark 0x100/0xf00 table 100 prio 110
    # wan2
    ip rule add fwmark 0x200/0xf00 table 200 prio 110
}

SET_PPTP_RULE() {
    # PPTP DNS Rule
    USE_PEERDNS=$(nvram get pptp_client_peerdns)
    if [ "$USE_PEERDNS" = "1" ]; then
    PPTP_GET_DNS=$(nvram get pptp_client_get_dns)
    for PPTP_DNS in $PPTP_GET_DNS
    do
       if [ ! "$PPTP_DNS" = "0.0.0.0" ]; then
           ip rule add to $PPTP_DNS table 250 prio 149
       fi
    done
    fi
}

BUILD_ROUTE_LB() {
    $LOGGER "set default router with load-balance"

    pppid=0    
    lbstr="ip route replace default scope global "
    while [ $pppid -le $WANNUM ]
    do
        pppif=ppp$pppid
        GET_PPP_GATEWAY $pppif
        if [ "$PPP_GATEWAY" != "" ]; then
            lbstr="$lbstr nexthop via $PPP_GATEWAY dev $pppif"
        fi
        ip route delete default
        let pppid=pppid+1
    done    
    echo $lbstr
    $lbstr
    
    $LOGGER "clear route cache..."
    ip route flush cache    
}

$LOGGER "Start PolicyRouting ..."

if [ -f $XWAN_OK ]; then
  BUILD_ROUTE_LB
  $LOGGER "Start PolicyRouting exit, XWAN_OK."
  exit
fi
if [ -f $LOCK_FILE ]; then
  $LOGGER "Start PolicyRouting exit, other process starting."
  exit
fi

echo "SETTING" > $LOCK_FILE

$LOGGER "disable rp_filter "
echo '0' >/proc/sys/net/ipv4/conf/all/rp_filter

if [ "$1" = "" ]; then
  ALLIFUP
else
    ALLIFDOWN
fi

sleep 10

#1. set ip rules
ip rule flush
ip rule del table main
ip rule del table default
ip rule add lookup main prio 32766
ip rule add lookup default prio 32767

#2. 设置走WAN1的静态路由(路由表来自网页设置"WAN2"自定义路由表)
SET_FIXED_ROUTE 1

#3. 设置每个WAN口的路由表
wanid=1
while [ $wanid -le 2 ]
do
    BUILD_WAN_ROUTETAB $wanid
    let wanid=wanid+1
done

SET_ASSIGNOUT_RULE
SET_PPTP_RULE
BUILD_ROUTE_LB

$LOGGER "Start PolicyRouting success."
echo "OK" > $XWAN_OK
rm $LOCK_FILE


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 路由
相关日志:
评论: 0 | 引用: 0 | 查看次数: 6275
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 3000 字 | UBB代码 开启 | [img]标签 开启