找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 11771|回复: 20

[负载平衡] ADSL三线同网关NTH分流+断线自动切换脚本

[复制链接]
发表于 2008-11-10 16:11:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
/ ip firewall mangle
add chain=prerouting in-interface="lan" connection-state=new nth=2,1,0 \
    action=mark-connection new-connection-mark=wan1_Routing_Mark passthrough=yes comment="" \
    disabled=no
add chain=prerouting in-interface="lan" connection-mark=wan1_Routing_Mark action=mark-routing \
    new-routing-mark=wan1_Routing_Mark passthrough=no comment="" disabled=no

add chain=prerouting in-interface="lan" connection-state=new nth=2,1,1 \
    action=mark-connection new-connection-mark=wan2_Routing_Mark passthrough=yes comment="" \
    disabled=no
add chain=prerouting in-interface="lan" connection-mark=wan2_Routing_Mark action=mark-routing \
    new-routing-mark=wan2_Routing_Mark passthrough=no comment="" disabled=no

add chain=prerouting in-interface="lan" connection-state=new nth=2,1,2 \
    action=mark-connection new-connection-mark=wan3_Routing_Mark passthrough=yes comment="" \
    disabled=no
add chain=prerouting in-interface="lan" connection-mark=wan3_Routing_Mark action=mark-routing \
    new-routing-mark=wan3_Routing_Mark passthrough=no comment="" disabled=no


关键NTH的设置,可以在论坛中搜索关键字“NTH”

根据我的理解,多线的设置如下:
如果有2条线路,那么NTH取值就是1,1,0/1,1,1
如果有4条线路,那么NTH取值就是3,1,0/3,1,1/3,1,2/3,1,3
如果有5条线路,那么NTH取值就是4,1,0/4,1,1/4,1,2/4,1,3/4,1,4

设置结果见附件

[ 本帖最后由 jackycao 于 2008-11-10 16:34 编辑 ]
标记脚本.JPG
mangle.jpg
routeros
 楼主| 发表于 2008-11-10 16:19:06 | 显示全部楼层

三线路由脚本

:local ip1 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out1"] address]
:local ip2 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out2"] address]
:local ip3 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out3"] address]
:set ip1 [:pick $ip1 0 ([:len $ip1]-3)]
:set ip2 [:pick $ip2 0 ([:len $ip2]-3)]
:set ip3 [:pick $ip3 0 ([:len $ip3]-3)]
/ip add add address=($ip1 . "/32") interface="pppoe-out1" comment= "wan1" disabled=no
/ip add add address=($ip2 . "/32") interface="pppoe-out2" comment= "wan2" disabled=no
/ip add add address=($ip3 . "/32") interface="pppoe-out3" comment= "wan3" disabled=no
/ ip firewall nat
add chain=srcnat connection-mark=wan1_Routing_Mark action=src-nat to-addresses=$ip1 \
    to-ports=0-65535 comment="wan1" disabled=no
add chain=srcnat connection-mark=wan2_Routing_Mark action=src-nat to-addresses=$ip2 \
    to-ports=0-65535 comment="wan2" disabled=no
add chain=srcnat connection-mark=wan3_Routing_Mark action=src-nat to-addresses=$ip3 \
    to-ports=0-65535 comment="wan3" disabled=no
/ ip route
add dst-address=0.0.0.0/0 gateway=$ip1 scope=255 target-scope=10 routing-mark=wan1_Routing_Mark \
    comment="wan1" disabled=no
add dst-address=0.0.0.0/0 gateway=$ip2 scope=255 target-scope=10 routing-mark=wan2_Routing_Mark \
    comment="wan2" disabled=no
add dst-address=0.0.0.0/0 gateway=$ip3 scope=255 target-scope=10 routing-mark=wan3_Routing_Mark \
    comment="wan3" disabled=no


我的拨号线路名称是pppoe-out*,标记是wan*
三线路由脚本.jpg
三线路由.JPG
routeros
回复

使用道具 举报

 楼主| 发表于 2008-11-10 16:21:42 | 显示全部楼层

自动切换脚本

:local ip1 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out1"] address]
:local ip2 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out2"] address]
:local ip3 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out3"] address]
:local ip4 [/ip add get [/ip add find comment= "wan1"] network]
:local ip5 [/ip add get [/ip add find comment= "wan2"] network]
:local ip6 [/ip add get [/ip add find comment= "wan3"] network]
:local ip7 [/ip fir nat get [/ip fir nat find connection-mark=wan1_Routing_Mark] to-addresses]
:local ip8 [/ip fir nat get [/ip fir nat find connection-mark=wan2_Routing_Mark] to-addresses]
:local ip9 [/ip fir nat get [/ip fir nat find connection-mark=wan3_Routing_Mark] to-addresses]
:local ip10 [/ip route get [/ip route find routing-mark=wan1_Routing_Mark] gateway]
:local ip11 [/ip route get [/ip route find routing-mark=wan2_Routing_Mark] gateway]
:local ip12 [/ip route get [/ip route find routing-mark=wan3_Routing_Mark] gateway]
:set ip1 [:pick $ip1 0 ([:len $ip1]-3)]
:set ip2 [:pick $ip2 0 ([:len $ip2]-3)]
:set ip3 [:pick $ip3 0 ([:len $ip3]-3)]
:if ($ip1 != $ip4) do={/ip add set [/ip add find comment="wan1"] address ($ip1 . "/32") broa $ip1 network $ip1}
:if ($ip1 != $ip7) do={/ip fir nat set [/ip fir nat find connection-mark=wan1_Routing_Mark] to-addresses $ip1}
:if ($ip1 != $ip10) do={/ip route set [/ip route find routing-mark=wan1_Routing_Mark] gateway $ip1}
:if ($ip2 != $ip5) do={/ip add set [/ip add find comment="wan2"] address ($ip2 . "/32") broa $ip2 network $ip2}
:if ($ip2 != $ip8) do={/ip fir nat set [/ip fir nat find connection-mark=wan2_Routing_Mark] to-addresses $ip2}
:if ($ip2 != $ip11) do={/ip route set [/ip route find routing-mark=wan2_Routing_Mark] gateway $ip2}
:if ($ip3 != $ip6) do={/ip add set [/ip add find comment="wan3"] address ($ip3 . "/32") broa $ip3 network $ip3}
:if ($ip3 != $ip9) do={/ip fir nat set [/ip fir nat find connection-mark=wan3_Routing_Mark] to-addresses $ip3}
:if ($ip3 != $ip12) do={/ip route set [/ip route find routing-mark=wan3_Routing_Mark] gateway $ip3}


自动切换脚本

从各个地方取值,然后做比较,如果不同就更新旧的ip地址。
routeros
回复

使用道具 举报

 楼主| 发表于 2008-11-10 16:24:50 | 显示全部楼层

启动运行脚本

:delay 10
/ip address remove wan1
/ip address remove wan2
/ip address remove wan3
/ip firewall nat remove wan1
/ip firewall nat remove wan2
/ip firewall nat remove wan3
/ip route remove wan1
/ip route remove wan2
/ip route remove wan3
:local ip1 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out1"] address]
:local ip2 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out2"] address]
:local ip3 [/ip add get [/ip add find broa=0.0.0.0 interface="pppoe-out3"] address]
:set ip1 [:pick $ip1 0 ([:len $ip1]-3)]
:set ip2 [:pick $ip2 0 ([:len $ip2]-3)]
:set ip3 [:pick $ip3 0 ([:len $ip3]-3)]
/ip add add address=($ip1 . "/32") interface="pppoe-out1" comment= "wan1" disabled=no
/ip add add address=($ip2 . "/32") interface="pppoe-out2" comment= "wan2" disabled=no
/ip add add address=($ip3 . "/32") interface="pppoe-out3" comment= "wan3" disabled=no
/ ip firewall nat
add chain=srcnat connection-mark=wan1_Routing_Mark action=src-nat to-addresses=$ip1 \
    to-ports=0-65535 comment="wan1" disabled=no
add chain=srcnat connection-mark=wan2_Routing_Mark action=src-nat to-addresses=$ip2 \
    to-ports=0-65535 comment="wan2" disabled=no
add chain=srcnat connection-mark=wan3_Routing_Mark action=src-nat to-addresses=$ip3 \
    to-ports=0-65535 comment="wan3" disabled=no
/ ip route
add dst-address=0.0.0.0/0 gateway=$ip1 scope=255 target-scope=10 routing-mark=wan1_Routing_Mark \
    comment="wan1" disabled=no
add dst-address=0.0.0.0/0 gateway=$ip2 scope=255 target-scope=10 routing-mark=wan2_Routing_Mark \
    comment="wan2" disabled=no
add dst-address=0.0.0.0/0 gateway=$ip3 scope=255 target-scope=10 routing-mark=wan3_Routing_Mark \
    comment="wan3" disabled=no


延迟10秒钟,等待adsl拨号成功,删除原先的配置,重新应用新的配置。
启动运行.JPG
routeros
回复

使用道具 举报

 楼主| 发表于 2008-11-10 16:28:43 | 显示全部楼层

自动执行

启动运行设置成startup的话显示的时间是06:28:15

另一个ip切换的脚本每2秒钟执行一次,让它循环就行了
启动运行11.jpg
routeros
回复

使用道具 举报

 楼主| 发表于 2008-11-10 16:32:13 | 显示全部楼层
具体做法参考了很多论坛上的帖子 ,不一一注明了,现在还剩下的问题就是网银的使用,搜索了一下是标记443端口的连接,具体做法还要再研究。
routeros
回复

使用道具 举报

 楼主| 发表于 2008-11-10 16:46:44 | 显示全部楼层
设置自动运行,命令行如下

[admin@MikroTik] system scheduler> set 要自动运行的脚本名称  start-time=startup


结果如图,在winbox里显示的时间是06:28:15
test.jpg
routeros
回复

使用道具 举报

发表于 2008-11-10 16:49:14 | 显示全部楼层
好东西...支持下.
routeros
回复

使用道具 举报

发表于 2008-11-14 18:59:45 | 显示全部楼层
好样的,这样完整共享出来的不多哦
routeros
回复

使用道具 举报

发表于 2008-11-14 19:07:17 | 显示全部楼层
有一条掉线了怎么办?楼主这个没有考虑进去哦!!1
routeros
回复

使用道具 举报

 楼主| 发表于 2008-11-15 07:15:41 | 显示全部楼层



恩,断线重拨换ip的问题考虑到了,如果一条线路坏掉这个没有做,思路是再做个双线的规则,如果某条pppoe线路拨不上去则自动禁用所有三线脚本,应用双线的脚本。

不过我们这里电信还是蛮稳定的,很少出现这种问题。
routeros
回复

使用道具 举报

发表于 2009-1-2 22:02:38 | 显示全部楼层
这么好的东西没有人顶?我顶
routeros
回复

使用道具 举报

发表于 2009-1-3 18:12:35 | 显示全部楼层
整天卖弄这些老东西

3。X 上面已经不能这样做了
routeros
回复

使用道具 举报

 楼主| 发表于 2009-1-7 07:42:54 | 显示全部楼层
整天卖弄这些老东西

3。X 上面已经不能这样做了
crack_ros 发表于 2009-1-3 18:12


卖弄个p啊,自己做好了发个心得,爱看就看,又没人逼你看。
routeros
回复

使用道具 举报

发表于 2009-7-7 08:48:41 | 显示全部楼层
支持楼主
整天卖弄这些老东西

3。X 上面已经不能这样做了
crack_ros 发表于 2009-1-3 18:12
这个傻B,自己整不来就算,你来这里乱说一通,就算你懂再多起什么作用,憋死你!
routeros
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|软路由 ( 渝ICP备15001194号-1|渝公网安备 50011602500124号 )

GMT+8, 2024-5-18 11:33 , Processed in 0.088439 second(s), 5 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表