为什么80%的码农都做不了架构师?>>>
本教程在网件wndr4300(openwrt-18.06.1)配置成功,本教程系博主原创,转载请保留源地址:https://my.oschina.net/kmwzjs/blog/2223214
一、资料地址
- 参考1,参考vlan设置、mwan3规则 http://koolshare.cn/thread-10566-1-1.html http://koolshare.cn/thread-10566-1-1.html
- 参考2,设置wan设置、部分脚本、mwan3规则 http://koolshare.cn/forum.php?mod=viewthread&tid=129366&highlight=%E5%A4%9A%E7%BA%BF
- 权威中国IP段数据获取 http://ftp.apnic.net/apnic/stats/apnic/`delegated-apnic-latest
- ip.cn网址的中国电信的ip段 https://ip.cn/chnroutes.html
如何将ip段换成所需格式
- 将权威中国ip转换
curl 'http://ftp.apnic.net/apnic/stats/apnic/`delegated-apnic-latest'
cat delegated-apnic-latest | awk -F '|' '/CN/&&/ipv4/ {print $4 "/" 32-log($5)/log(2)}'|cat >ip.txt
- 将ip.cn的电信ip转换
curl 'http://f.ip.cn/rt/isproutes-ct.txt' |grep -v "routes" > telecom.txt
二、原理说明
- 最近几年电信出国网速越来越慢,而移动却很快,经实验,移动宽带可以流畅看youtube的4k视频
- 通过VLAN设置,将其中一个lan口设置成第二个wan口,命名为
wan2
- 然后通过ipset配合mwan3规则,设置电信ip段走带wan口(电信),其他走wan2(移动)
- 如果开机启动的脚本中有访问网络内容,就必须放到pppoe拨号事件里,而不能放到开机启动脚本,参考:https://my.oschina.net/kmwzjs/blog/687408
三、VLAN设置,将其中一个lan口变成wan2口并拨号成功
参考资料地址步骤设置lan变wan2口,注意将接口wan2也设置成pppoe并设置拨号信息,高级设置:wan跃点40,wan2跃点41,注意下面事项:
- 做VLAN划分的时候,拔网线可以及时看到对应网口
- 交换机设置中,每一行都是一个VLAN分组,每个VLAN分组内的接口都是桥接在一起的,每个端口在分组中有三个选项: 关、不标记、已标记。
- 关: 这一分组中不使用这个接口
- 不标记: 这个接口将被直接桥接到这个分组
- 已标记: 这个接口需要通过VLAN ID来访问这一分组
四、openwrt基本设置,用ssh链接
先设置wan的pppoe拨号为移动账户,电信网络 opkg update 有问题,ssh连接路由器(linksys wrt 1900ACS刷后需要将ssh链接指定接口)
opkg update
# 安装中文luci包
opkg install luci-i18n-base-zh-cn
# 安装mwan3
opkg install mwan3
# 安装mwan3z中文luci包
opkg install luci-app-mwan3
opkg install luci-i18n-mwan3-zh-cn
# 安装curl
opkg install curl
并设置时区为Asia/shanghai,成功后将wan换回电信,wan2换回移动
五、mwan3(负载均衡)设置
注意:
- 规则有顺序之分,最上面优先级别最高
- mwan3(负载均衡)最后的规则改成目标地址:0.0.0.0/0
- 注意设置“接口成员的跟踪的主机或IP地址”这样才能正确显示负载均衡的状态
六、开机自动负载分流、计划任务(自动更新电信ip段)
- 新建
vi /root/route.sh
脚本作用是下载ip.cn的中国电信段数据,然后转换成ipset的配置文件载入
#!/bin/sh
ipurl="http://f.ip.cn/rt/isproutes-ct.txt"
info=`curl -s -m 10 --connect-timeout 10 -I $ipurl`
code=`echo $info|grep "HTTP"|awk '{print $2}'`
if [ "$code" == "200" ];then
curl $ipurl |grep -v "routes" > /root/telecom.txt
fi
ipsetconf=/root/ipset.conf
echo -n "" > $ipsetconf
ROUTES=/root/telecom.txt
for network in `cat $ROUTES`; do
echo "add telecom $network" >> $ipsetconf
done
ipset flush telecom
ipset restore -f /root/ipset.conf
- 然后设置可执行权限
chmod +x /root/route.sh
- 设置openwrt的启动项中或者编辑
/etc/rc.local
,意为:新建一个命为telecom
的ipset规则
ipset -N telecom nethash
- 加入计划任务,定时更新规则
0 2 * * 1 /bin/sh /root/route.sh
- 因为开机启动的
/root/route.sh
需要访问网络,需要在拨号成功后执行(参考),新建vi /etc/hotplug.d/iface/99-ipreport
,内容如下:
#!/bin/bash
if [ $ACTION=ifup -a $INTERFACE=wan ]; then
sh /root/route.sh
mwan3 restart
fi
$INTERFACE=wan 后面的wan可以换成启动接口,含义为wan拨号成功后执行/root/route.sh脚本(脚本需要下载ip段文件),mwan3的选项卡通知
似乎也可以设置,待验证...
- 重启路由器,ssh登录后,执行下面命令看到大量ip即成功,可以访问一个国内电信或者国外地址确认是否成功,国内地址可能不会正确显示电信地址,因为可能做了域名智能解析
ipset list
六、关闭mwan3
后台界面没有关闭按钮,需要ssh登录执行:
mwan3 stop
七、其他
openwrt 最新版不支持4300的128M,解决方案:https://routeragency.com/?p=260
未通过...
设置访客网络ssid:http://www.right.com.cn/forum/thread-173844-1-1.html
单线多拨:https://lixingcong.github.io/2018/04/13/mwan3-macvlan-notes/ 只需要设置到pppoe拨号成功就行了,后面通过mwan3规则来设置不同ip使用不同的公网ip上网