2014年12月27日星期六

Openwrt配置:自动多拨脚本及杂项

本文的目的是说明使用脚本实现
  1. 自动多拨
  2. 多拨完成后关闭未连接的pppoe端口(防止自动重复拨号,耗费CPU资源)
  3. 重设mwan3的https连接

系统: CHAOS CALMER (Bleeding Edge, r42871)
前提条件:
  1. 设置好了多个wan(wan1, wan2, wan3, wan4, ...,我这里是7个),并可以多拨;
  2. 安装了mwan3,并已配置好防火墙等(可参考下面的mwan3配置文件);

使用crontab定时运行多拨脚本;

这里采用的多拨方法是network restart,然后检查拨号成功的数目,当到达所需的多拨个数时停止;
我这里一般7个wan可以播出2-3个,运气好能有5个;

一共用了3个脚本
  1. MultiDialPPPOE.sh:用于多拨,接受1个参数,就是想要达到的多拨数目;
  2. CloseDeadLink.sh:用于多拨完成后关闭未拨号成功的wan,这样防止这些wan自动重拨,占用CPU资源
  3. SetHTTPS4mwan3.sh:用于重设mwan3的443端口的策略,保证https连接可用(对于网银等很重要);

为了重设mwan3的https配置,使用了两个临时文件来重新组合成mwan3的配置文件:mwan3.part1和mwan3.part2

定时运行多拨(每天早上3:20拨号,其中MultiDialPPPOE.sh 3即表示需要达到的多拨数目,达到3个或以上拨号后就停止)
root@OpenWrtZZH:/etc/crontabs# cat /etc/crontabs/root
20 03 * * * sh /home/MultiDialShell/MultiDialPPPOE.sh 3 >> /home/MultiDialShell/PPPOE_MultiDial.log

下面是 MultiDialPPPOE.sh内容:

root@OpenWrtZZH:/home/MultiDialShell# cat MultiDialPPPOE.sh
#!/bin/sh
echo "**************************************************************"
echo "`date +%Y%m%d-%H%M%S`"
# NUMBER OF PPPOE LINKS
if [ "$1" = "" ]
then
    NumOfLinkRequired=1
else
    NumOfLinkRequired=$1
fi

# GET ACTUAL PPPOE LINK NUMBER.
ActualLinkNum=`ifconfig | grep 'pppoe-wan' | wc -l`

while [ $ActualLinkNum -lt $NumOfLinkRequired ]
do
    echo "        $ActualLinkNum(Actual) of $NumOfLinkRequired(required) links"
    echo "        trying to restart network..."
    echo "        COMMAND: /etc/init.d/network restart"
    /etc/init.d/network restart
    sleep 5
   
    ActualLinkNum=`ifconfig | grep 'pppoe-wan' | wc -l`
done

echo "    $ActualLinkNum(Actual) / $NumOfLinkRequired(Required) links!"

sleep 1
echo "    Closing Dead Links..."
sh ./CloseDeadLink.sh
sleep 1
echo "    Resetting HTTPS links for mwan3..."
sh ./SetHTTPS4mwan3.sh
echo "Dial Proc Done! "

echo "**************************************************************"
下面是 CloseDeadLink.sh内容:
root@OpenWrtZZH:/home/MultiDialShell# cat CloseDeadLink.sh
#!/bin/sh
# close the dead link of wan*

TempValue=1

for k in $( seq 1 7 )
do
    string=pppoe-wan$k
    wanstatus=`ifconfig | grep $string | wc -l`
    if [ $wanstatus -ne $TempValue ]
        then
        ifdown wan$k
        echo "        wan$k stopped"
    else
        echo "        wan$k active"
    fi
下面是 SetHTTPS4mwan3.sh内容:
root@OpenWrtZZH:/home/MultiDialShell# cat SetHTTPS4mwan3.sh
#!/bin/sh
# close the dead link of wan*

TempValue=1
# wan1 PPPOE status check

for k in $( seq 1 7 )
do
    string=pppoe-wan$k
    echo "        checking wan$k"
    wanstatus=`ifconfig | grep $string | wc -l`
    if [ $wanstatus -eq $TempValue ]
        then
        echo "" > mwan3.test
        str1=w${k}_only
        str2="'"
        str3="option use_policy "
        str4=${str3}${str2}${str1}${str2}
       
        cat mwan3.part1 > mwan3.test
        echo $str4 >> mwan3.test
        cat mwan3.part2 >> mwan3.test
        echo "        wan${k} is active"
        echo "        Setting wan${k} as https port..."
        break
    fi
done
cp mwan3.test /etc/config/mwan3
sleep 1
mwan3 restart
下面是完整的mwan3内容(/etc/config下面)
root@OpenWrtZZH:/home/MultiDialShell# cat mwan3.bk20141228

config interface 'wan1'
    option enabled '1'
    option reliability '2'
    option count '1'
    option timeout '3'
    option interval '8'
    option down '8'
    option up '8'
    option reroute '1'

config interface 'wan2'
    option enabled '1'
    option reliability '2'
    option count '1'
    option timeout '3'
    option interval '8'
    option down '8'
    option up '8'
    option reroute '1'

config interface 'wan3'
    option enabled '1'
    option reliability '2'
    option count '1'
    option timeout '3'
    option interval '8'
    option down '8'
    option up '8'
    option reroute '1'

config interface 'wan4'
    option enabled '1'
    option reliability '2'
    option count '1'
    option timeout '3'
    option interval '8'
    option down '8'
    option up '8'
    option reroute '1'

config interface 'wan5'
    option enabled '1'
    option reliability '2'
    option count '1'
    option timeout '3'
    option interval '8'
    option down '8'
    option up '8'
    option reroute '1'

config interface 'wan6'
    option enabled '1'
    option reliability '2'
    option count '1'
    option timeout '3'
    option interval '8'
    option down '8'
    option up '8'
    option reroute '1'

config interface 'wan7'
    option enabled '1'
    option reliability '2'
    option count '1'
    option timeout '3'
    option interval '8'
    option down '8'
    option up '8'
    option reroute '1'


config member 'w1_m1_w1'
    option interface 'wan1'
    option metric '1'
    option weight '1'

config member 'w2_m1_w1'
    option interface 'wan2'
    option metric '1'
    option weight '1'

config member 'w3_m1_w1'
    option interface 'wan3'
    option metric '1'
    option weight '1'

config member 'w4_m1_w1'
    option interface 'wan4'
    option metric '1'
    option weight '1'

config member 'w5_m1_w1'
    option interface 'wan5'
    option metric '1'
    option weight '1'

config member 'w6_m1_w1'
    option interface 'wan6'
    option metric '1'
    option weight '1'

config member 'w7_m1_w1'
    option interface 'wan7'
    option metric '1'
    option weight '1'


config policy 'w1_only'
    list use_member 'w1_m1_w1'

config policy 'w2_only'
    list use_member 'w2_m1_w1'

config policy 'w3_only'
    list use_member 'w3_m1_w1'

config policy 'w4_only'
    list use_member 'w4_m1_w1'

config policy 'w5_only'
    list use_member 'w5_m1_w1'

config policy 'w6_only'
    list use_member 'w6_m1_w1'

config policy 'w7_only'
    list use_member 'w7_m1_w1'


config policy 'wan_s_balanced'
    list use_member 'w1_m1_w1'
    list use_member 'w2_m1_w1'
    list use_member 'w3_m1_w1'
    list use_member 'w4_m1_w1'
    list use_member 'w5_m1_w1'
    list use_member 'w6_m1_w1'
    list use_member 'w7_m1_w1'


config rule 'rule_https'
    option dest_port '443'
    option proto 'tcp'
    option use_policy 'w2_only'

config rule 'rule_all'
    option use_policy 'wan_s_balanced'
    option proto 'all'


没有评论:

发表评论