找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 13336|回复: 14

Tomato Multiwan 多拨情况下负载均衡与策略路由

[复制链接]
本帖最后由 Kim_Kong_♂ 于 2014-1-15 23:18 编辑
  1. #!/bin/sh
  2. #-------------------------------
  3. #      Created by Tommy.Liu
  4. #      
  5. #-------------------------------

  6. #------------自行修改-----------
  7. wan1_if=ppp0
  8. wan1_ipaddr=$(ifconfig $wan1_if|grep "inet addr"|awk -F ":" '{print $2}'|awk -F " " '{print $1}')
  9. wan1_gateway=$(ifconfig $wan1_if|grep "inet addr"|awk -F ":" '{print $3}'|awk -F " " '{print $1}')

  10. wan2_if=ppp1
  11. wan2_ipaddr=$(ifconfig $wan2_if|grep "inet addr"|awk -F ":" '{print $2}'|awk -F " " '{print $1}')
  12. wan2_gateway=$(ifconfig $wan2_if|grep "inet addr"|awk -F ":" '{print $3}'|awk -F " " '{print $1}')
  13. #-------------------------------

  14. echo "Wan1 IP:$wan1_ipaddr"
  15. echo "Wan1 Gateway:$wan1_gateway"
  16. echo "Wan2 IP:$wan2_ipaddr"
  17. echo "Wan2 Gateway:$wan2_gateway"

  18. echo "Set adv routing..."
  19. #设定策略路由
  20. ip rule flush
  21. ip rule add lookup main prio 32766
  22. ip rule add lookup default prio 32767
  23. ip rule add from $wan1_ipaddr table 100 prio 100
  24. ip rule add fwmark 0x100 table 100 prio 101
  25. ip rule add from $wan2_ipaddr table 200 prio 200
  26. ip rule add fwmark 0x200 table 200 prio 201
  27. ip route flush table 100
  28. ip route flush table 200
  29. for TABLE in 100 200
  30. do
  31.    ip route | grep link | while read ROUTE
  32.    do
  33.       ip route add table $TABLE to $ROUTE
  34.    done
  35. done
  36. ip route add table 100 default via $wan1_gateway
  37. ip route add table 200 default via $wan2_gateway


  38. echo "Set PREROUTING..."
  39. #清除PREROUTING
  40. iptables -t mangle -F PREROUTING
  41. #所有wan1进入的包(NEW)都标记为0x100,走Wan1策略路由(101)
  42. iptables -t mangle -A PREROUTING -i $wan1_if -m state --state NEW -j CONNMARK --set-mark 0x100
  43. #所有wan2进入的包(NEW)都标记为0x200,走Wan2策略路由(201)
  44. iptables -t mangle -A PREROUTING -i $wan2_if -m state --state NEW -j CONNMARK --set-mark 0x200
  45. #所有LAN进入并且已经建立连接的包,恢复MARK
  46. iptables -t mangle -A PREROUTING -i br0 -m state --state RELATED,ESTABLISHED -j CONNMARK --restore-mark

  47. echo "Set POSTROUTING..."
  48. #清除POSTROUTING
  49. iptables -t mangle -F POSTROUTING
  50. #所有WAN1出去的包
  51. iptables -t mangle -A POSTROUTING -o $wan1_if -m state --state NEW -j CONNMARK --set-mark 0x100
  52. #所有WAN2出去的包
  53. iptables -t mangle -A POSTROUTING -o $wan2_if -m state --state NEW -j CONNMARK --set-mark 0x200

  54. #DNS默认走WAN1(应为双线的线路如果不是同一个ISP,那么DNS就不能混用,只能指定一个)
  55. iptables -t mangle -A POSTROUTING -p udp --dport 53 -j CONNMARK --set-mark 0x100


  56. echo "Set QOS..."
  57. #QOS
  58. iptables -t mangle -F OUTPUT
  59. iptables -t mangle -A OUTPUT -o $wan1_if -j QOSO
  60. iptables -t mangle -A OUTPUT -o $wan2_if -j QOSO

  61. iptables -t mangle -F FORWARD
  62. iptables -t mangle -A FORWARD -o $wan1_if -j QOSO
  63. iptables -t mangle -A FORWARD -o $wan2_if -j QOSO

  64. echo "Set Nat..."

  65. echo "Set default gateway..."
  66. #负载平衡
  67. ip route change default equalize nexthop via $wan1_gateway nexthop via $wan2_gateway

  68. echo "finished."
复制代码

我的恩山、我的无线 The best wifi forum is right here.
本帖最后由 E网咖啡/tp 于 2014-1-16 21:20 编辑

感谢提供,不知道在哪加入
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

应该是启动脚本里面
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

本帖最后由 qzl520 于 2014-1-18 15:57 编辑

菜鸟看不明白负载均衡代码,能注释下就好了我是三拨,请问是修改成这样吗?
如果修改对的话,这个脚本添加到哪里呢?

  1. #------------三拨修改的可对啊?-----------
  2. wan1_if=ppp0
  3. wan1_ipaddr=$(ifconfig $wan1_if|grep "inet addr"|awk -F ":" '{print $2}'|awk -F " " '{print $1}')
  4. wan1_gateway=$(ifconfig $wan1_if|grep "inet addr"|awk -F ":" '{print $3}'|awk -F " " '{print $1}')

  5. wan2_if=ppp1
  6. wan2_ipaddr=$(ifconfig $wan2_if|grep "inet addr"|awk -F ":" '{print $2}'|awk -F " " '{print $1}')
  7. wan2_gateway=$(ifconfig $wan2_if|grep "inet addr"|awk -F ":" '{print $3}'|awk -F " " '{print $1}')

  8. wan3_if=ppp2
  9. wan3_ipaddr=$(ifconfig $wan3_if|grep "inet addr"|awk -F ":" '{print $2}'|awk -F " " '{print $1}')
  10. wan3_gateway=$(ifconfig $wan3_if|grep "inet addr"|awk -F ":" '{print $3}'|awk -F " " '{print $1}')
  11. #-------------------------------

  12. echo "Wan1 IP:$wan1_ipaddr"
  13. echo "Wan1 Gateway:$wan1_gateway"
  14. echo "Wan2 IP:$wan2_ipaddr"
  15. echo "Wan2 Gateway:$wan2_gateway"
  16. echo "Wan3 IP:$wan3_ipaddr"
  17. echo "Wan3 Gateway:$wan3_gateway"

  18. echo "Set adv routing..."
复制代码

我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
qzl520 发表于 2014-1-18 15:51
菜鸟看不明白负载均衡代码,能注释下就好了我是三拨,请问是修改成这样吗?
如果修改对的话,这个脚本添加 ...
  1. #########################################################################
  2. # File Name: route.sh
  3. # Author: Kim Kong
  4. # mail: kongqingzhang@gmail.com
  5. # Created Time:  5E5  1/12 10:51:14 2014
  6. #########################################################################
  7. #!/bin/bash

  8. wan1_if=ppp0
  9. wan1_ip=$(ifconfig ppp0 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1)
  10. wan1_gw=$(ifconfig ppp0 | grep "inet addr" | cut -d":" -f3 | cut -d" " -f1)
  11. wan2_if=ppp1
  12. wan2_ip=$(ifconfig ppp1 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1)
  13. wan2_gw=$(ifconfig ppp1 | grep "inet addr" | cut -d":" -f3 | cut -d" " -f1)
  14. wan3_if=ppp2
  15. wan3_ip=$(ifconfig ppp2 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1)
  16. wan3_gw=$(ifconfig ppp2 | grep "inet addr" | cut -d":" -f3 | cut -d" " -f1)

  17. echo "Wan1 IP:$wan1_ip"
  18. echo "Wan1 Gateway:$wan1_gw"
  19. echo "Wan2 IP:$wan2_ip"
  20. echo "Wan2 Gateway:$wan2_gw"
  21. echo "Wan3 IP:$wan3_ip"
  22. echo "Wan3 Gateway:$wan3_gw"

  23. echo "Set adv routing..."

  24. sleep 20
  25. ip rule flush
  26. ip rule add lookup main prio 32766
  27. ip rule add lookup default prio 32767
  28. ip rule add from $wan1_ip table 50 prio 50
  29. ip rule add fwmark 0x100 table 50 prio 51
  30. ip rule add from $wan2_ip table 100 prio 100
  31. ip rule add fwmark 0x200 table 100 prio 101
  32. ip rule add from $wan3_ip table 150 prio 150
  33. ip rule add fwmark 0x300 table 150 prio 151
  34. ip route flush table 50
  35. ip route flush table 100
  36. ip route flush table 150
  37. for TABLE in 50 100 150
  38. do
  39. ip route | grep link | while read ROUTE
  40.   do
  41.    ip route add table $TABLE to $ROUTE
  42.   done
  43. done
  44. ip route add table 50 default via $wan1_gw dev ppp0 src $wan1_ip
  45. ip route add table 100 default via $wan2_gw dev ppp1 src $wan2_ip
  46. ip route add table 150 default via $wan3_gw dev ppp2 src $wan3_ip
  47. ip route delete default


  48. echo "Set PREROUTING..."

  49. iptables -t mangle -F PREROUTING

  50. iptables -t mangle -A PREROUTING -i $wan1_if -m state --state NEW -j CONNMARK --set-mark 0x100

  51. iptables -t mangle -A PREROUTING -i $wan2_if -m state --state NEW -j CONNMARK --set-mark 0x200

  52. iptables -t mangle -A PREROUTING -i $wan3_if -m state --state NEW -j CONNMARK --set-mark 0x300

  53. iptables -t mangle -A PREROUTING -i br0 -m state --state RELATED,ESTABLISHED -j CONNMARK --restore-mark

  54. echo "Set POSTROUTING..."

  55. iptables -t mangle -F POSTROUTING

  56. iptables -t mangle -A POSTROUTING -o $wan1_if -m state --state NEW -j CONNMARK --set-mark 0x100

  57. iptables -t mangle -A POSTROUTING -o $wan2_if -m state --state NEW -j CONNMARK --set-mark 0x200

  58. iptables -t mangle -A POSTROUTING -o $wan3_if -m state --state NEW -j CONNMARK --set-mark 0x300


  59. iptables -t mangle -A POSTROUTING -p udp --dport 53 -j CONNMARK --set-mark 0x100
  60. iptables -t mangle -A POSTROUTING -p udp --dport 8000 -j CONNMARK --set-mark 0x100



  61. echo "Set Nat..."

  62. echo "Set default gateway..."
  63. ip route add default scope global equalize nexthop via $wan1_gw dev ppp0 weight 1 nexthop via $wan2_gw dev ppp1 weight 1 nexthop via $wan3_gw dev ppp2 weight 1

  64. echo "finished."
复制代码
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| | 显示全部楼层
qzl520 发表于 2014-1-18 15:51
菜鸟看不明白负载均衡代码,能注释下就好了我是三拨,请问是修改成这样吗?
如果修改对的话,这个脚本添加 ...

这样QQ不掉线,旺旺还是掉线,因为旺旺走tcp,我正在找解决的办法
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

我去试一试,多谢
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

问下楼主,我是ADSL 拨号+中继转有线接入,要怎么修改这个脚本!!WAN为拨号+WAN1为中继转有线接入
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

本帖最后由 olsl 于 2014-11-12 14:32 编辑

贴出我的4wan ,我的第一个是WAN不是WAN1,根据自己的情修改
  1. #########################################################################
  2. # File Name: route.sh
  3. # Author: Kim Kong
  4. # mail: kongqingzhang@gmail.com
  5. # Created Time:  5E5  1/12 10:51:14 2014
  6. #########################################################################
  7. #!/bin/bash

  8. wan_if=ppp0
  9. wan_ip=$(ifconfig ppp0 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1)
  10. wan_gw=$(ifconfig ppp0 | grep "inet addr" | cut -d":" -f3 | cut -d" " -f1)
  11. wan2_if=ppp1
  12. wan2_ip=$(ifconfig ppp1 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1)
  13. wan2_gw=$(ifconfig ppp1 | grep "inet addr" | cut -d":" -f3 | cut -d" " -f1)
  14. wan3_if=ppp2
  15. wan3_ip=$(ifconfig ppp2 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1)
  16. wan3_gw=$(ifconfig ppp2 | grep "inet addr" | cut -d":" -f3 | cut -d" " -f1)
  17. wan4_if=ppp3
  18. wan4_ip=$(ifconfig ppp3 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1)
  19. wan4_gw=$(ifconfig ppp3 | grep "inet addr" | cut -d":" -f3 | cut -d" " -f1)

  20. echo "wan IP:$wan_ip"
  21. echo "wan Gateway:$wan_gw"
  22. echo "Wan2 IP:$wan2_ip"
  23. echo "Wan2 Gateway:$wan2_gw"
  24. echo "Wan3 IP:$wan3_ip"
  25. echo "Wan3 Gateway:$wan3_gw"
  26. echo "Wan4 IP:$wan4_ip"
  27. echo "Wan4 Gateway:$wan4_gw"

  28. echo "Set adv routing..."

  29. sleep 20
  30. ip rule flush
  31. ip rule add lookup main prio 32766
  32. ip rule add lookup default prio 32767
  33. ip rule add from $wan_ip table 50 prio 50
  34. ip rule add fwmark 0x100 table 50 prio 51
  35. ip rule add from $wan2_ip table 100 prio 100
  36. ip rule add fwmark 0x200 table 100 prio 101
  37. ip rule add from $wan3_ip table 150 prio 150
  38. ip rule add fwmark 0x300 table 150 prio 151
  39. ip rule add from $wan4_ip table 200 prio 200
  40. ip rule add fwmark 0x400 table 200 prio 201
  41. ip route flush table 50
  42. ip route flush table 100
  43. ip route flush table 150
  44. ip route flush table 200
  45. for TABLE in 50 100 150 200
  46. do
  47. ip route | grep link | while read ROUTE
  48.   do
  49.    ip route add table $TABLE to $ROUTE
  50.   done
  51. done
  52. ip route add table 50 default via $wan_gw dev ppp0 src $wan_ip
  53. ip route add table 100 default via $wan2_gw dev ppp1 src $wan2_ip
  54. ip route add table 150 default via $wan3_gw dev ppp2 src $wan3_ip
  55. ip route add table 200 default via $wan4_gw dev ppp3 src $wan4_ip
  56. ip route delete default


  57. echo "Set PREROUTING..."

  58. iptables -t mangle -F PREROUTING

  59. iptables -t mangle -A PREROUTING -i $wan_if -m state --state NEW -j CONNMARK --set-mark 0x100

  60. iptables -t mangle -A PREROUTING -i $wan2_if -m state --state NEW -j CONNMARK --set-mark 0x200

  61. iptables -t mangle -A PREROUTING -i $wan3_if -m state --state NEW -j CONNMARK --set-mark 0x300

  62. iptables -t mangle -A PREROUTING -i $wan4_if -m state --state NEW -j CONNMARK --set-mark 0x400

  63. iptables -t mangle -A PREROUTING -i br0 -m state --state RELATED,ESTABLISHED -j CONNMARK --restore-mark

  64. echo "Set POSTROUTING..."

  65. iptables -t mangle -F POSTROUTING

  66. iptables -t mangle -A POSTROUTING -o $wan_if -m state --state NEW -j CONNMARK --set-mark 0x100

  67. iptables -t mangle -A POSTROUTING -o $wan2_if -m state --state NEW -j CONNMARK --set-mark 0x200

  68. iptables -t mangle -A POSTROUTING -o $wan3_if -m state --state NEW -j CONNMARK --set-mark 0x300

  69. iptables -t mangle -A POSTROUTING -o $wan4_if -m state --state NEW -j CONNMARK --set-mark 0x400

  70. iptables -t mangle -A POSTROUTING -p udp --dport 53 -j CONNMARK --set-mark 0x100
  71. iptables -t mangle -A POSTROUTING -p udp --dport 8000 -j CONNMARK --set-mark 0x100



  72. echo "Set Nat..."

  73. echo "Set default gateway..."
  74. ip route add default scope global equalize nexthop via $wan_gw dev ppp0 weight 1
  75. ip route add default scope global equalize nexthop via $wan_gw dev ppp0 weight 1 nexthop via $wan2_gw dev ppp1 weight 1
  76. ip route add default scope global equalize nexthop via $wan_gw dev ppp0 weight 1 nexthop via $wan2_gw dev ppp1 weight 1 nexthop via $wan3_gw dev ppp2 weight 1
  77. ip route add default scope global equalize nexthop via $wan_gw dev ppp0 weight 1 nexthop via $wan2_gw dev ppp1 weight 1 nexthop via $wan3_gw dev ppp2 weight 1 nexthop via $wan4_gw dev ppp3 weight 1




  78. echo "finished."
复制代码



不知为何最后一项,写在一起有时加不进去,只能分开多加几次了
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

我把他加入到了wanUP中,成功
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

不错帮顶一下了
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

怎么强制某个ip 走某个wan?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

这么复杂????
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

这么简单?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

能说下这个策略是用来干什么的吗?
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

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

本版积分规则

关闭

欢迎大家光临恩山无线论坛上一条 /1 下一条

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-5-5 21:54

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

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