|
发表于 2009-6-1 23:27:06
|
显示全部楼层
本帖最后由 fanxk 于 2009-6-1 23:29 编辑
转 此文来自网上,没有修改也没有验证 查看文章
xADSL+RANDOM负载均衡 完美自适应2009-03-17 15:32软件环境:routeros 2.9.27
功能:实现不同带宽多条ADSL的负载均衡和会聚
第一步, 在/interface里新建好PPPOE client填好ADSL用户名和密码,在原来的name前面加上此条的带宽,
如:2mpppoe-out1,4mpppoe-out2,6mpppoe-out6,不要修改name的其它地方.在不要有多余的PPPOE client,
不要勾ADD default route(默认是勾的),清空ROUTES表NAT表和MANGLE表(自己要加的脚本运行完了再加)
第二步, 全自动配置 请修改内网段,默认为 10.10.10.0/24
脚本复制到WINBOX的New Terminal终端粘贴即可(不包括分隔线).
---------------------------------分隔线------------------------------------------------
:local dd "10.10.10.0/24"
:local x [:len [/int fi type="pppoe-out"]]
:local xx
:local b 1
/ip fir nat add chain=srcnat src-address=$dd action=masquerade \
comment="all" disabled=yes
/ ip fir man add chain=prerouting src-address=$dd protocol=tcp dst-port=443 \
acti=mark-routing new-routing-mark=r1 passthrough=yes comment="443port"
:foreach a in=[/int fi type="pppoe-out" ] do={
/int set $a com=$b
:set b ($b+1) }
:for i from=1 to=$x do={
:set xx ($i . "." . $i . "." . $i . "." . $i)
/ ip ad add add=($xx . "/32") netw=$xx broa=$xx \
inte=([/in ge [/in fi com=$i] name]) com=$i
/ ip fir man add chain=prerouting src-address=$dd connection-state=new \
action=mark-connec new-connection-mark=("c" . $i) com=("con" . $i)
/ ip fir man add chain=prerouting src-address=$dd connection-mark=("c" . $i) \
action=mark-rout new-routing-mark=("r" . $i) passthrough=no com=("rou" . $i)
/ ip ro add dst-add=0.0.0.0/0 gatew=$xx routing-m=("r" . $i) com=$i check=ping
/ ip fir nat add chain=srcnat src-address=$dd com=$i action=src-nat \
connection-m=("c" . $i) to-address=$xx to-ports=0-65535 }
/ip fir man add chain=forward protocol=tcp tcp-flags=syn \
action=change-mss new-mss=1400 comment="ChangMSS1400"
/ ip ro add dst-add=0.0.0.0/0 gatew=1.1.1.1 com=($x+1) check=ping
=================================分隔线==============================================
第三步,创建自适应脚本 将脚本复制到WINBOX的New Terminal终端粘贴即可(不包括分隔线)
----------------------------------分隔线----------------------------------------------
/ system scheduler
add name="Xadsl" start-time=00:00:00 interval=20s on-event={
:global x [:len [/int fi type="pppoe-out"]]
:local nad
:local oad
:local face
:local am 0
:local cc 0
:local rm
:local disx
:local ll [:len [/interface find running=true type="pppoe-out"]]
:local pt [/int find running=true type="pppoe-out"]
:local pf [/int find running=false type="pppoe-out"]
:for i from=1 to=$x do={
:if ([/int get [/int find com=$i] running] =true) do={
:set face [/int get [/int find com=$i] name]
:set nad [/ip ad get [/ip ad find dyna=true inte=$face] add]
:set nad [:pick $nad 0 [:find $nad "/"]]
:set oad [/ip add get [/ip add find comment=$i] addr]
:set oad [:pick $oad 0 [:find $oad "/"]]
:if ($oad != $nad) do={
/ip ad set [/ip ad find comm=$i] addr=$nad netw=$nad broa=$nad
/ip route set [/ip route find comment=$i] gateway=$nad
/ip fi nat set [/ip fi nat find comm=$i] to-address=$nad
} } }
:if ($ll>=2) do={
:foreach i in=[/int fi type="pppoe-out" running=yes ] do={
:set am ($am+([:pick ([/int get $i name]) 0 1 ])) }
:for i from=1 to=($ll -1) do={
:set rm [:pick ([/int get [:pick $pt ($i -1) $i] name]) 0 1 ]
:set rm (($rm * 1000) / $am)
:if ([:pick $rm 2 3] >5) do={ :set rm ($rm+10) }
:set rm ($rm /10)
/ip fi man set [/ip fir man fi com=("con" . $i)] random=$rm
:set am ($am -([:pick ([/int get [:pick $pt ($i -1) $i] name]) 0 1])) }
/ip fi ma unset [/ip fi ma fi com=("con" . $ll)] random
:if ([/ip fi na get [/ip fi na fi com=all] disa] =no) do={
/ip fi na set [/ip fi na fi com=all] disa=yes }
:if ([/ip fi ma get [/ip fi ma fi com=("con" . $ll)] disa] =true) do={
:for i from=1 to=$ll do={
/ip fi ma set [/ip fi ma fi com=("con" . $i)] disa=no
/ip fi ma set [/ip fi ma fi com=("rou" . $i)] disa=no } }
:if ([/ip fi ma get [/ip fi ma fi com=("con" . ($ll+1))] disa] =no) do={
:for i from=1 to=($x - $ll) do={
/ip fi ma set [/ip fi ma fi com=("con" . ($x -($i -1)))] disa=yes
/ip fi ma set [/ip fi ma fi com=("rou" . ($x -($i -1)))] disa=yes } }
:for i from=1 to=($x - $ll) do={
/ip fir nat set [/ip fir nat find com=[/int get \
[:pick $pf ($i -1) $i] com]] disa=yes
/ip rou set [/ip rou find com=[/int get \
[:pick $pf ($i -1) $i] com]] disa=yes }
:for i from=1 to=$x do={
:set cc ($cc +[:len [/ip ro get [/ip ro fi com=$i] routing-m]]) }
:set disx [:len [/ip ro fi disa=true]]
:if (($disx !=($x - $ll))||($cc != ($ll*2))) do={
:for i from=1 to=$ll do={
/ip fi na set [/ip fi na fi com=[/int get \
[:pick $pt ($i -1) $i] com]] disa=no
/ip fi na set [/ip fir nat find com=[/int get \
[:pick $pt ($i -1) $i] com]] connection-mark=("c" . $i)
/ip ro set [/ip ro find com=[/int get \
[:pick $pt ($i -1) $i] com]] disa=no
/ip ro set [/ip ro fi com=[/int get \
[:pick $pt ($i -1) $i] com]] routing-mark=("r" . $i) }}
:for i from=1 to=($x - $ll) do={
/ip fi na unset [/ip fi na fi com=[/int get \
[:pick $pf ($i -1) $i] com]] connection-mark
/ip ro unset [/ip ro fi com=[/int get \
[:pick $pf ($i -1) $i] com]] routing-m }
}
:set disx [:len [/ip ro fi disa=true]]
:if ($ll=1) do={
:if ([/ip fi na get [/ip fi na fi com=all] disa] =yes) do={
/ip fi na set [/ip fi na fi com=all] disa=no }
:if ($disx != $x) do={
:for i from=1 to=$x do={
/ip fi na set [/ip fi na fin com=$i] disa=yes
/ip ro set [/ip ro fi com=$i] disa=yes
/ip ro unset [/ip ro fi com=$i] routing-mark } }
}
:delay 2s
:if ([/ip ro get [/ip ro fi com=($x+1)] active] != true) do={
:set nad [/ip add get [/int get [/int fi runn=true \
type=pppoe-out] comm] netw]
/ip ro set [/ip ro fi com=($x+1)] gatew=$nad }
} |
|