Linux双线路切换(一)

一. 实现双线路策略
流量分割
  让我们先定义一些符号。令第一块网卡的名字叫eth1,而第二块网卡叫做eth2;然后设置网卡1的IP地址为219.150.222.36(CTC分配网址),网卡2 的IP地址为125.42.176.199(CNC分配网址);ISP1(电信)网关地址为219.150.222.33,ISP2(网通)网关地址为125.42.176.193;最后,ISP1(电信)的网络地址为219.150.222.32/27,ISP2(网通)的网络地址为125.42.176.192/27。
额外创建两个路由表,50 和51 ,加入到/etc//iproute2/rt_tables中。然后如下设置两个路由表中的路由:
ip route add 219.150.222.32/27 dev eth1 src 219.150.222.36 table 50
ip route add default via 219.150.222.33  table 50
 
ip route add 125.42.176.192/27 dev eth2 src 125.42.176.199 table 51
ip route add default via 125.42.176.193  table 51
主机设置
配置如下文件:
 Wan1.cong:
interface=eth0
ipaddr=219.150.222.36
gateway=219.150.222.33
network=219.150.222.32/27
routefile=/etc/quick/IP_CTC.list(电信网段)
配置ETH0,用于连接电信链路连接。
wan2.cong:
interface=eth1
ipaddr=125.42.176.199
gateway=125.42.176.193
network=125.42.176.192/27
routefile=/etc/quick/IP_CNC.list(网通网段)
配置ETH1,用于连接网通链路连接。
 
routefile= 后面填该线路对应的路由表文件。IP_CNC.list 和IP_CTC.list,这两个文件都在/etc/quick 目录下,其中IP_CNC.list 文件是网通路由表文件,IP_CTC.list 为电信路由表文件。
IP_CTC.list的内容如下:

58.32.0.0/13
58.40.0.0/15
58.42.0.0/16
58.43.0.0/16
58.44.0.0/14
58.48.0.0/13
58.56.0.0/14
58.60.0.0/14
58.208.0.0/12
59.32.0.0/13
59.40.0.0/15
59.42.0.0/16
59.43.0.0/16
59.44.0.0/14
59.48.0.0/16
59.49.0.0/17
59.49.128.0/17
59.50.0.0/16
59.51.0.0/17
59.51.128.0/17
59.52.0.0/14
59.56.0.0/14
59.60.0.0/15
59.62.0.0/15
60.160.0.0/15
60.162.0.0/15
60.164.0.0/15
60.166.0.0/15
60.168.0.0/13
60.176.0.0/12
61.130.0.0/15
61.132.0.0/15
61.134.0.0/18
61.134.64.0/19
61.136.128.0/17
61.137.0.0/17
61.138.192.0/18
61.139.0.0/17
61.139.192.0/18
61.140.0.0/14
61.144.0.0/14
61.150.0.0/15
61.152.0.0/14
61.157.0.0/16
61.159.64.0/18
61.159.128.0/17
61.160.0.0/16
61.161.64.0/18
61.164.0.0/15
61.166.0.0/16
61.169.0.0/16
61.170.0.0/15
61.172.0.0/14
61.177.0.0/16
61.178.0.0/16
61.180.0.0/17
61.183.0.0/16
61.184.0.0/14
61.188.0.0/16
61.189.128.0/17
61.190.0.0/15
124.72.0.0/16
124.73.0.0/16
124.74.0.0/15
124.76.0.0/14
124.112.0.0/15
125.64.0.0/13
125.72.0.0/16
125.73.0.0/16
125.74.0.0/15
125.76.0.0/17
125.77.0.0/16
125.78.0.0/15
125.80.0.0/13
125.88.0.0/13
125.104.0.0/13
125.112.0.0/12
202.96.96.0/21
202.96.104.0/21
202.96.112.0/20
202.96.128.0/21
202.96.136.0/21
202.96.144.0/20
202.96.160.0/21
202.96.168.0/21
202.96.176.0/20
202.96.192.0/16
202.97.0.0/19
202.97.32.0/19
202.97.64.0/19
202.97.96.0/19
202.98.32.0/19
202.98.64.0/19
202.98.96.0/21
202.98.104.0/21
202.98.112.0/20
202.98.128.0/19
202.98.160.0/19
202.98.192.0/19
202.98.224.0/19
202.99.192.0/19
202.100.96.0/19
202.100.128.0/19
202.100.160.0/19
202.100.192.0/18
202.101.0.0/18
202.101.64.0/19
202.101.96.0/19
202.101.128.0/18
202.101.192.0/18
202.102.0.0/17
202.103.0.0/16
202.104.0.0/15
202.107.128.0/17
202.109.0.0/16
202.110.128.0/18
202.111.0.0/17
218.0.0.0/14
218.4.0.0/15
218.6.0.0/16
218.13.0.0/16
218.14.0.0/15
218.16.0.0/15
218.18.0.0/16
218.19.0.0/16
218.20.0.0/16
218.21.0.0/17
218.22.0.0/15
218.31.0.0/16
218.32.0.0/16
218.62.128.0/17
218.63.0.0/16
218.64.0.0/15
218.66.0.0/16
218.67.0.0/17
218.70.0.0/15
218.72.0.0/15
218.74.0.0/16
218.75.0.0/16
218.76.0.0/16
218.77.0.0/16
218.78.0.0/15
218.80.0.0/12
219.128.0.0/12
219.144.0.0/13
219.152.0.0/15
219.159.64.0/18
219.159.128.0/17
220.160.0.0/11
221.224.0.0/13
221.232.0.0/14
221.236.0.0/15
221.238.0.0/16
221.239.0.0/17
221.239.128.0/17
222.64.0.0/13
222.72.0.0/15
222.74.0.0/16
222.75.0.0/16
222.76.0.0/14
222.80.0.0/15
222.82.0.0/16
222.83.0.0/17
222.83.128.0/17
222.84.0.0/16
222.85.0.0/17
222.85.128.0/17
222.86.0.0/15
222.88.0.0/15
222.90.0.0/15
222.92.0.0/14
222.168.0.0/15
222.170.0.0/16
222.171.0.0/16
222.172.0.0/17
222.172.128.0/17
222.173.0.0/16
222.174.0.0/15
222.176.0.0/13
222.184.0.0/13
222.208.0.0/13
222.216.0.0/15
222.218.0.0/16
222.219.0.0/16
222.220.0.0/15
222.222.0.0/15
222.240.0.0/13

IP_CNC.list的内容如下:

58.16.0.0/16
58.17.0.0/17
58.17.128.0/17
58.18.0.0/16
58.19.0.0/16
58.20.0.0/16
58.22.0.0/15
58.240.0.0/15
58.242.0.0/15
58.246.0.0/15
58.248.0.0/13
60.0.0.0/13
60.8.0.0/15
60.10.0.0/16
60.11.0.0/16
60.12.0.0/16
60.13.0.0/18
60.13.128.0/17
60.14.0.0/15
60.16.0.0/13
60.24.0.0/14
60.28.0.0/15
60.30.0.0/16
60.31.0.0/16
60.208.0.0/13
60.216.0.0/15
60.218.0.0/15
60.220.0.0/14
61.48.0.0/13
61.133.0.0/17
61.134.96.0/19
61.134.128.0/17
61.135.0.0/16
61.137.128.0/17
61.138.0.0/17
61.138.128.0/18
61.139.128.0/18
61.148.0.0/15
61.156.0.0/16
61.159.0.0/18
61.161.0.0/18
61.161.128.0/17
61.162.0.0/16
61.163.0.0/16
61.167.0.0/16
61.168.0.0/16
61.176.0.0/16
61.179.0.0/16
61.181.0.0/16
61.182.0.0/16
61.189.0.0/17
202.96.0.0/18
202.96.64.0/21
202.96.72.0/21
202.97.128.0/18
202.97.224.0/21
202.97.240.0/20
202.98.0.0/21
202.98.8.0/21
202.99.64.0/19
202.99.96.0/21
202.99.128.0/19
202.99.160.0/21
202.99.168.0/21
202.99.176.0/20
202.99.208.0/20
202.99.224.0/21
202.99.232.0/21
202.99.240.0/20
202.102.128.0/21
202.102.224.0/21
202.102.232.0/21
202.106.0.0/16
202.107.0.0/17
202.108.0.0/16
202.110.0.0/17
202.111.128.0/18
203.93.8.0/24
203.93.192.0/18
210.13.128.0/17
210.14.160.0/19
210.14.192.0/19
210.15.32.0/19
210.15.96.0/19
210.15.128.0/18
210.21.0.0/16
210.52.128.0/17
210.53.0.0/17
210.53.128.0/17
210.74.96.0/19
210.74.128.0/19
210.82.0.0/15
218.8.0.0/14
218.12.0.0/16
218.21.128.0/17
218.24.0.0/14
218.56.0.0/14
218.60.0.0/15
218.67.128.0/17
218.68.0.0/15
218.104.0.0/14
219.154.0.0/15
219.156.0.0/15
219.158.0.0/17
219.158.128.0/17
219.159.0.0/18
220.252.0.0/16
221.0.0.0/15
221.2.0.0/16
221.3.0.0/17
221.3.128.0/17
221.4.0.0/16
221.5.0.0/17
221.5.128.0/17
221.6.0.0/16
221.7.0.0/19
221.7.32.0/19
221.7.64.0/19
221.7.96.0/19
221.8.0.0/15
221.10.0.0/16
221.11.0.0/17
221.11.128.0/18
221.11.192.0/19
221.12.0.0/17
221.12.128.0/18
221.13.0.0/18
221.13.64.0/19
221.13.96.0/19
221.13.128.0/17
221.14.0.0/15
221.192.0.0/15
221.194.0.0/16
221.195.0.0/16
221.196.0.0/15
221.198.0.0/16
221.199.0.0/19
221.199.32.0/20
221.199.128.0/18
221.199.192.0/20
221.200.0.0/14
221.204.0.0/15
221.206.0.0/16
221.207.0.0/18
221.207.64.0/18
221.207.128.0/17
221.208.0.0/14
221.212.0.0/16
221.213.0.0/16
221.216.0.0/13
222.128.0.0/14
222.132.0.0/14
222.136.0.0/13
222.160.0.0/15
222.162.0.0/16
222.163.0.0/19

文件/root/cdkcm,脚本的内容:
. /root/hs
RETVAL=0
start() {
if [ -f "/root/wan1.cong" ] #加载网卡0的配置
then
. /root/wan1.cong
tab=50
wan_cdk
fi
if [ -f "/root/wan2.cong" ] #加载网卡1的配置
then
. /root/wan2.cong
tab=51
wan_cdk
fi
ip route flush cache
        return $RETVAL
}
 
stop() {
if [ -f "/root/wan1.cong" ]
then
. /root/wan1.cong
tab=50
del_wan_cdk
fi
 
if [ -f "/root/wan2.cong" ]
then
. /root/wan2.cong
tab=51
del_wan_cdk
fi
 
ip route flush cache
    return $RETVAL
}
 
# See how we were called.
case "$1" in
  start)
    start
    RETVAL=$?
    ;;
  stop)
    stop
    RETVAL=$?
    ;;
  restart)
    stop
    start
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart}"
    exit 1
esac
 
exit $RETVAL
 
/root/hs文件内容(函数存放文件)
wan_cdk() {
  ip route add ${network} dev ${interface} src ${ipaddr} table $tab
  ip route add default via ${gateway} dev ${interface} table $tab
  ip rule add from ${ipaddr} table $tab
    for cc in `/bin/cat ${routefile}`; do
  ip rule add from all to $cc table $tab
    done
}
 
del_wan_cdk() {
  ip rule del from ${ipaddr} table $tab
    for cc in `/bin/cat ${routefile}`; do
  ip rule del from all to $cc table $tab
    done
  ip route del ${network} dev ${interface} src ${ipaddr} table $tab
  ip route del default via ${gateway} dev ${interface} table $tab
}
Hs主要用于加载路由表,实现流量分隔。
负载均衡
第二个问题就是如何对通过两个ISP流出的数据进行负责均衡。如果你已经成功地实现了流量分割。这件事不难。
与选择两个ISP中的一个作为缺省路由不同,这次是设置缺省路由为多线路路由。在缺省内核中,这会均衡两个ISP的路由。像下面这样做(基于前面的流量分割实验):
ip route add default scope global nexthop via 219.150.222.33 dev eth1 weight 30 nexthop via 125.42.176.193 dev eth2 weight 70  (注意:与上面为同一行命令)
这样可以均衡两个ISP的路由。通过调整“weight”参数我们可以指定其中的一个ISP的优先权高于另一个。(具体:线路1的流量约占30%,线路1占70%)
不过负载均衡有一个不好的地方是,在网上打游戏的时候经常被卡掉,由于路由的智能选择,经常进行线路切换,导致游戏无法正常进行。所以我经常做的方法是把缺省路由改为单一的电信路由ip route add default via 219.150.222.36或者是网通ip route add default via 125.42.176.193。这样既可以实现对不同网络提供商的网站进行有效访问,又不会导致你的游戏经常掉线。当然,假如你现在的缺省路由是电信的,但是你要用QQ与对方的网通网络进行文件传输,这还是很慢的。
设置IP伪装,对内网机子进行NAT转换
iptables -t nat -A POSTROUTING  -o eth0 -s 192.168.0.0/24 -j SNAT --to 219.150.222.36
iptables -t nat -A POSTROUTING -o eth1 -s 192.168.0.0/24 -j SNAT --to 125.42.176.199
打开转发功能
Echo “1”>/proc/sys/net/ipv4/ip_forward
或者修改/etc/sysctl.conf文件内容,将:net.ipv4.ip_forward=”设置为1
由于RHEL AS4有防火墙,我们需要清除冲突的规则
A.清除原有防火墙规则
IPTABLES �CP INPUT ACCEPT
IPTABLES �CP FORWARD ACCEPT
IPTABLES �CP OUTPUT ACCEPT
B.清除NAT规则设置
IPTABLES �Ct nat �CP PREROUTING ACCEPT
IPTABLES �Ct nat �CP POSTROUTING ACCEPT
IPTABLES �Ct nat �CP OUTPUT ACCEPT
IPTABLES �Ct mangle �CP PREROUTING ACCEPT
IPTABLES �Ct mangle �CP OUTPUT ACCEPT
C.清除在防火墙和NAT中不是默认的连接
IPTABLES �CF
IPTABLES �Ct nat �CF
IPTABLES �Ct mangle �CF
IPTABLES �CX
IPTABLES �Ct nat �CX
IPTABLES �Ct mangle -X
保存iptables配置文件 :  /etc/rc.d/init.d/iptables save
或者 service iptables save
至此,既可实现双线路策略。
要注意清除原有的防火墙策略,这有可能导致无法实现NAT转换功能

你可能感兴趣的:(linux,网络,职场,切换,休闲)