请选择 进入手机版 | 继续访问电脑版

网络科技

    今日:16| 主题:284709
收藏本版
互联网、科技极客的综合动态。

[其他] Double your Internet speed: Load Balancing made easy

[复制链接]
lzxohgzxf 发表于 2016-10-3 06:02:21
155 2
Ever wanted to combine two WAN connections for faster* internet? I do, for torrenting. And I scripted it.
  On the Web, you’ll easily find “tutorials” that just give you some bash script you have to painstakingly modify for it to run. This is as simple as it gets!    But see for yourself!(code at the end of this post)  
  Since I get a dynamic (external) IP right into my PC, that would mean, I’d have to modify that every time. So I made this small script, that only depends on    iproute2, which you’ll need anyways, and    awkand    grep, that should already be on your system.  
  Just run the script as root and give the network interfaces you want to balance as parameters.
  1. ~$ sudo ./load-balance.sh eth0 eth1 wlan0
复制代码
In a future version, I’d like to add support for dynamically setting the weight (currently, all interfaces get the same priority).
  My setup looks like this:
  
       
  •       eth0goes to my Uni’s Interweb (they are my ISP)   
  •       eth1is connected via a USB-Ethernet-adapter to a router with a UMTS-modem   
  •       wlan0is my Android phone in tethering mode  
  As you can see, it is of course necessary, that you have multiple entry points into the Internet. If you connect your PC with multiple connections to the same WAN, you won’t get any speed increase.
  * Load balancing won’t work in all use cases. If you for example just stream a single video (or download a single file), only one of the balanced interfaces will be in use, and no speed increase is visible. But when you download multiple things, like the chunks of a torrent, half of the files/chunks are downloaded over the first IF, and half over the other.
      See on GitHub Gist  

  1. #!/bin/bash

  2. # Load balance multiple internet connections. Requires iproute2, awk and grep.
  3. # (C) 2016 Tobias Girstmair, isticktoit.net, GPLv2
  4. # Also useful: speedometer -l  -r eth1 -t eth1 -m $(( 1024 * 1024 * 3 / 2 ))
  5. # Not much user error checking is done - only pass working network connections

  6. # script needs root to work and at least two connections to be useful
  7. [ $EUID -eq 0 -a $# -ge 2 ] || {
  8.         echo "Usage (as root): $0 iface1 iface2 ..." >&2
  9.         exit 1
  10. }

  11. get_free_tblnum() { # http://stackoverflow.com/a/28702075
  12.         awk -v RS='\\s+' '{ a[$1] } END { for(i = 10; i in a; ++i); print i }'> /etc/iproute2/rt_tables
  13.         fi
  14.         MY_IP=$(ip -o -4 addr show $IFACE |awk -F'(\\s|/)+' '{print $4}')
  15.         GW_IP=$(ip route show dev $IFACE | awk '/default/ {print $3}')
  16.         SUBNT=$(ip route show dev eth0 | awk '/proto kernel/ {print $1}')

  17.         ip route add $SUBNT dev $IFACE src $MY_IP table $TABLE
  18.         ip route add default via $GW_IP table $TABLE
  19.         ip rule add from $MY_IP table $TABLE
  20.         echo nexthop via $GW_IP dev $IFACE weight 1
  21. }

  22. ip route add default scope global $(for IF in "$@"; do loadbal $IF; done)
复制代码
草兮丶 发表于 2016-10-3 08:18:45
友情提示:由于此用户的签名太过于个性而被系统自动屏蔽
回复 支持 反对

使用道具 举报

yh7jt 发表于 2016-10-5 02:24:14
☆:签到是人气、荣誉的比拼,让我们的签到见证一份坚持吧!!!
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表