文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>电信、网通双线自切

电信、网通双线自切

时间:2005-09-22  来源:wulang2005

电信、网通双线自切

同段ip切换速度会自己向下掉啊掉~保持不住,不稳定

                Internet
                  |     |
                  |     |
                电信 网通
                  |     |
                     |
                 交换机
                    |
              eth1  eth2
                 Server
                   eth0
                      |
           _____________
       客户机  客户机  客户机  客户机192.168.0.0/24 网关192.168.0.254

其中eth0 ip 192.168.0.0/24
     eth1 222.168.1.3/255.255.255.252
     eth2 218.62.3.3/255.255.255.252
     默认网关为网通的218.62.3.2
     加上ip route add的电信网关 222.168.1.2

部分脚本为
iptables -t nat -I POSTROUTING -s 192.168.0.0/24 -d $电信ip -j SNAT --to 222.168.1.3
***
***
***
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to 218.62.3.3

这样呢就实现了一个网段的电信网通自动切换

前期测试:如果单独切换ip,内网同时并存192.168.0.254/24 192.168.2.254/24两个网关,速度很稳定,可以实现预期效果

现在我有这么一个想法,如下:
加入 eth0:1,ip为192.168.2.254/24
ip rule 192.168.2.254,配合着上边电信的路由~

下边的客户机依然用192.168.0.254做默认网关

当客户机访问电信ip段时,能否自动通过192.168.0.254这个网关转到192.168.2.254这个网关,以实现最佳的预期效果呢?

route add -net 192.168.0.0 netmask 255.255.255.0 dev 192.168.2.254

代码:

# echo "200    DIANXIN" >> /etc/iproute2/rt_table
# ip route add default via 222.168.1.2 table DIANXIN
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d $电信IP -j MARK --set-mark 1
# ip rule add fwmark 1 table DIANXIN
# ip route flush cache
# iptables -t nat -F
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE


参考文档:
http://iptables-tutorial.frozentux.net/cn/iptables-tutorial-cn-1.1.19.html
http://www.linuxaid.com.cn/engineer/ideal/article/policy_pouting.htm
http://bbs.chinaunix.net/forum/viewtopic.php?t=304033&highlight=platinum

你只顾伪装源地址,忘记了修改路由
我正好相反,只顾修改路由跳转,忘了正确伪装源地址

# echo "200    DIANXIN" >> /etc/iproute2/rt_table
# ip route replace default via 222.168.1.2 table DIANXIN
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d $电信IP -j MARK --set-mark 1
# ip rule add fwmark 1 table DIANXIN
# ip route flush cache
# iptables -t nat -F
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d $电信地址-j SNAT --to $电信网卡的WAN地址
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to $网通网卡的WAN地址

这样试试
# echo "200    DIANXIN" >> /etc/iproute2/rt_table
# ip route replace default via 222.168.1.2 table DIANXIN
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d $电信IP -j MARK --set-mark 1
# ip rule add fwmark 1 table DIANXIN
# ip route flush cache
# iptables -t nat -F
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d $电信地址-j SNAT --to $电信网卡的WAN地址
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to $网通网卡的WAN地址

哈哈哈哈

一切終於正常了

額且最有意思的就是與順序有關

得先

iptables -t mangle -I PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.222.5.0/15 -j MARK --set-mark 1
iptables -t mangle -I PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.240.0.0/13 -j MARK --set-mark 1

再這個

iptables -t nat -I POSTROUTING -s $PRIVATE -d 222.222.5.0/15 -j SNAT --to $DIANXIN
iptables -t nat -I POSTROUTING -s $PRIVATE -d 222.240.0.0/13 -j SNAT --to $DIANXIN

然後規則用

# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to $网通网卡的WAN地址

一切OK

如果點到了SNAT,呵呵.默認的網通就成電信了

有待捉摸
这也就是我为什么
iptables -t nat -F
的原因了

我没写mangle表,你也可以加一个
iptables -t mangle -F
然后把-I换成-A就可以了

我总结一下,正确的是

代码:

# echo "200    DIANXIN" >> /etc/iproute2/rt_table(这个是添加到文件,执行一次即可)
# ip route replace default via 222.168.1.2 table DIANXIN
# ip rule add fwmark 1 table DIANXIN(这个注意顺序,用ip rule可以查看)
# iptables -t nat -F
# iptables -t mangle -F
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.222.5.0/15 -j MARK --set-mark 1
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.240.0.0/13 -j MARK --set-mark 1
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 222.222.5.0/15 -j SNAT --to $DIANXIN
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 222.240.0.0/13 -j SNAT --to $DIANXIN
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to $接网通线路网卡的地址
# ip route flush cache


这样的结果,是访问222.222.5.0/15和222.240.0.0/13走电信网卡、电信路由,伪装成电信出口地址,其他默认网通

参考文档:
http://iptables-tutorial.frozentux.net/cn/iptables-tutorial-cn-1.1.19.html
http://www.linuxaid.com.cn/engineer/ideal/article/policy_pouting.htm
http://bbs.chinaunix.net/forum/viewtopic.php?t=304033&highlight=platinum

-A是append,是追加
-I是insert,是插入,默认插入到第一条

# echo "200    DIANXIN" >> /etc/iproute2/rt_tables(这个是添加到文件,执行一次即可)
# ip route replace default via 222.168.1.2 table DIANXIN(replace用来替换默认路由,而不是添加)
# ip rule add fwmark 1 table DIANXIN(这个注意顺序,用ip rule可以查看)
# iptables -t nat -F
# iptables -t mangle -F
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.222.5.0/15 -j MARK --set-mark 1
# iptables -t mangle -A PREROUTING -i eth0 -s 192.168.0.0/24 -d 222.240.0.0/13 -j MARK --set-mark 1
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 222.222.5.0/15 -j SNAT --to $DIANXIN
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 222.240.0.0/13 -j SNAT --to $DIANXIN
# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to $接网通线路网卡的地址
# ip route flush cache

这样的结果,是访问222.222.5.0/15和222.240.0.0/13走电信网卡、电信路由,伪装成电信出口地址,其他默认网通




相关阅读 更多 +
排行榜 更多 +
翌日波奇狗的历险记手机版下载

翌日波奇狗的历险记手机版下载

休闲益智 下载
怪兽远征安卓版下载

怪兽远征安卓版下载

角色扮演 下载
谷歌卫星地图免费版下载

谷歌卫星地图免费版下载

生活实用 下载