第一种方法
Vi /etc/rt.sh
#!/bin/bash
IF1=eth0
IP1=电信IP
P1_NET=电信网段/掩码
IP2=网通IP
P2_NET=网通网段/掩码
IP2_GW=网通网关
ip route add $P1_NET dev $IF1 src $IP1 table TEL
ip route add default via $IP1_GW table TEL
ip route add $P2_NET dev $IF2 src $IP2 table EDU
ip route add default via $IP2_GW table EDU
ip route add default via $IP1_GW
ip route flush cache
ip rule add from $IP1 table TEL
ip rule add from $IP2 table EDU
记住wq保存
vi /etc/iproute2/rt_tables
# reserved values
#255 local
#254 main
#253 default
#0 unspec
#
# local
#
#1 inr.ruhep
252 TEL
251 CNC
后wq保存
命令行下启用这个脚本:
chmod 750 /etc/rt.sh
命令行下每次运行这个脚本:
./etc/rt.sh
上面两条命令只对当前有效,当服务器重启时无效,为了让服务器每次重启系统后自动运行脚本则:
Vi /etc/rc.d/rc.local
/etc/rt.sh
这是一种方法,不一定是很好使,你可以试试
第二种方法
Vi /etc/rc.local
ip route add default via 电信网关 table 10
ip rule add from 电信IP table 10
ip route add default via 网通网关table 11
ip rule add from 网通IP table 11
第三种方法
vi /usr/local/bin/gw.sh
#!/bin/sh
/sbin/ip rule add from 网通网段/掩码 pref 10000 table cnc
/sbin/ip route add default via 网通网关 table cnc
/sbin/ip route flush cache
~
vi /usr/local/bin/cncip.sh
/sbin/ip ru ls|grep 20001:|awk '{print "ip ru del to", $5," pref 20001 table cnc"}'|sh
/sbin/ip rule add to 121.100.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 121.100.128.0/19 pref 20001 table cnc
/sbin/ip rule add to 121.16.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 121.24.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 121.28.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 121.30.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 121.31.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 121.51.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 121.89.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 122.0.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 122.198.192.0/18 pref 20001 table cnc
/sbin/ip rule add to 124.128.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 124.128.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.160.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.160.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.161.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.161.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.162.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.162.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.163.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.163.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.164.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 124.164.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.198.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 124.198.192.0/18 pref 20001 table cnc
/sbin/ip rule add to 124.21.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.248.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 124.254.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.64.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 124.66.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 124.67.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.88.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.89.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 124.89.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.89.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 124.90.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 124.90.0/16 pref 20001 table cnc
/sbin/ip rule add to 124.92.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 124.92.0/16 pref 20001 table cnc
/sbin/ip rule add to 125.32.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 125.33.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 125.34.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 125.35.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 125.35.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 125.36.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 125.40.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 202.102.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 202.102.224.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.106.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 202.107.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 202.108.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 202.110.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 202.110.192.0/18 pref 20001 table cnc
/sbin/ip rule add to 202.110.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 202.111.128.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.111.160.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.142.16.0/14 pref 20001 table cnc
/sbin/ip rule add to 202.142.16.0/20 pref 20001 table cnc
/sbin/ip rule add to 202.74.208.0/22 pref 20001 table cnc
/sbin/ip rule add to 202.74.8.0/20 pref 20001 table cnc
/sbin/ip rule add to 202.74.8.0/21 pref 20001 table cnc
/sbin/ip rule add to 202.75.144.0/20 pref 20001 table cnc
/sbin/ip rule add to 202.75.208.0/22 pref 20001 table cnc
/sbin/ip rule add to 202.75.212.0/22 pref 20001 table cnc
/sbin/ip rule add to 202.75.212/19 pref 20001 table cnc
/sbin/ip rule add to 202.76.160.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.96.64.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.97.128.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.97.160.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.97.192.0/18 pref 20001 table cnc
/sbin/ip rule add to 202.98.0.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.99.0.0/23 pref 20001 table cnc
/sbin/ip rule add to 202.99.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 202.99.192.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.99.224.0/19 pref 20001 table cnc
/sbin/ip rule add to 202.99.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 203.166.160.0/19 pref 20001 table cnc
/sbin/ip rule add to 203.93.0.0/25 pref 20001 table cnc
/sbin/ip rule add to 203.93.0.0/25 pref 20001 table cnc
/sbin/ip rule add to 203.93.141.0/24 pref 20001 table cnc
/sbin/ip rule add to 210.12.128.0/24 pref 20001 table cnc
/sbin/ip rule add to 210.12.192.0/24 pref 20001 table cnc
/sbin/ip rule add to 210.14.160.0/18 pref 20001 table cnc
/sbin/ip rule add to 210.21.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 210.21.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 210.22.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 210.51.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 210.52.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 210.52.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 210.53.8.0/23 pref 20001 table cnc
/sbin/ip rule add to 210.74.128.0/19 pref 20001 table cnc
/sbin/ip rule add to 210.74.96.0/19 pref 20001 table cnc
/sbin/ip rule add to 210.75.144.0/20 pref 20001 table cnc
/sbin/ip rule add to 210.76.160.0/19 pref 20001 table cnc
/sbin/ip rule add to 210.78.0.0/19 pref 20001 table cnc
/sbin/ip rule add to 210.82.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 210.83.0.0/20 pref 20001 table cnc
/sbin/ip rule add to 210.83.224.0/19 pref 20001 table cnc
/sbin/ip rule add to 211.152.32.0/19 pref 20001 table cnc
/sbin/ip rule add to 218.10.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 218.104.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 218.104.128.0/20 pref 20001 table cnc
/sbin/ip rule add to 218.104.160.0/19 pref 20001 table cnc
/sbin/ip rule add to 218.104.192.0/21 pref 20001 table cnc
/sbin/ip rule add to 218.104.200.0/21 pref 20001 table cnc
/sbin/ip rule add to 218.104.216.0/21 pref 20001 table cnc
/sbin/ip rule add to 218.104.224.0/19 pref 20001 table cnc
/sbin/ip rule add to 218.104.96.0/20 pref 20001 table cnc
/sbin/ip rule add to 218.106.112.0/20 pref 20001 table cnc
/sbin/ip rule add to 218.106.128.0/21 pref 20001 table cnc
/sbin/ip rule add to 218.106.144.0/20 pref 20001 table cnc
/sbin/ip rule add to 218.106.208.0/20 pref 20001 table cnc
/sbin/ip rule add to 218.106.96.0/22 pref 20001 table cnc
/sbin/ip rule add to 218.107.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 218.107.192.0/19 pref 20001 table cnc
/sbin/ip rule add to 218.11.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 218.12.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 218.21.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 218.24.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 218.26.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 218.27.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 218.28.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 218.56.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 218.60.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 218.62.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 218.67.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 218.68.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 218.7.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 218.8.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 219.154.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 219.156.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 219.158.0.0/20 pref 20001 table cnc
/sbin/ip rule add to 219.158.32.0/19 pref 20001 table cnc
/sbin/ip rule add to 219.159.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 220.114.204.0/18 pref 20001 table cnc
/sbin/ip rule add to 220.248.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 220.248.128.0/19 pref 20001 table cnc
/sbin/ip rule add to 220.248.128.0/20 pref 20001 table cnc
/sbin/ip rule add to 220.248.192.0/19 pref 20001 table cnc
/sbin/ip rule add to 220.249.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 220.249.192.0/18 pref 20001 table cnc
/sbin/ip rule add to 220.249.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 220.250.0.0/19 pref 20001 table cnc
/sbin/ip rule add to 220.252.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.0.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 221.10.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.11.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.11.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 221.11.192.0/19 pref 20001 table cnc
/sbin/ip rule add to 221.11.224.0/19 pref 20001 table cnc
/sbin/ip rule add to 221.12.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.12.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 221.13.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 221.13.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.13.64.0/19 pref 20001 table cnc
/sbin/ip rule add to 221.13.96.0/19 pref 20001 table cnc
/sbin/ip rule add to 221.14.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 221.192.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 221.196.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 221.198.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.199.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.199.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 221.199.192.0/20 pref 20001 table cnc
/sbin/ip rule add to 221.199.224.0/23 pref 20001 table cnc
/sbin/ip rule add to 221.200.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 221.2.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.204.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 221.206.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.207.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 221.207.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.207.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 221.208.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 221.213.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.214.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 221.216.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 221.3.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.3.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.4.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.5.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.5.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.6.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 221.7.0.0/19 pref 20001 table cnc
/sbin/ip rule add to 221.7.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 221.7.32.0/19 pref 20001 table cnc
/sbin/ip rule add to 221.7.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 221.8.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 222.128.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 222.132.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 222.136.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 222.160.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 58.16.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 58.17.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 58.17.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 58.18.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 58.19.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 58.20.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 58.21.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 58.22.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 58.240.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 58.242.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 58.244.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 58.246.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 58.248.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 58.250.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 58.252.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 58.83.0/22 pref 20001 table cnc
/sbin/ip rule add to 60.0.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 60.10.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 60.11.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 60.12.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 60.13.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 60.13.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 60.13.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 60.14.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 60.14.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 60.16.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 60.208.0.0/13 pref 20001 table cnc
/sbin/ip rule add to 60.216.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 60.218.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 60.220.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 60.24.0.0/14 pref 20001 table cnc
/sbin/ip rule add to 60.253.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 60.255.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 60.28.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 60.30.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 60.31.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 60.8.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 61.133.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 61.134.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.134.192.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.134.96.0/19 pref 20001 table cnc
/sbin/ip rule add to 61.135.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.136.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.136.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.137.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 61.138.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.138.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.138.64.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.139.128.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.148.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.156.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.158.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 61.158.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 61.159.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.161.0.0/18 pref 20001 table cnc
/sbin/ip rule add to 61.161.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 61.162.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.163.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.167.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.168.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.176.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.179.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.180.128.0/17 pref 20001 table cnc
/sbin/ip rule add to 61.181.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.182.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.189.0.0/17 pref 20001 table cnc
/sbin/ip rule add to 61.52.0.0/15 pref 20001 table cnc
/sbin/ip rule add to 61.54.0.0/16 pref 20001 table cnc
/sbin/ip rule add to 61.55.0.0/16 pref 20001 table cnc
/sbin/ip route flush cache
启动项加以下内容
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/local/bin/cncip.sh
/usr/local/bin/gw.sh
/sbin/modprobe ip_conntrack hashsize=2621400
/root/iptables.sh
加策略及NAT转换
vi /root/iptables.sh
#!/bin/bash
### load the mod ###
/sbin/modprobe ip_tables
/sbin/modprobe iptable_nat
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_nat_irc
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
####################
/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -t nat -F
/sbin/iptables -t nat -X
/sbin/iptables -t nat -Z
/sbin/iptables -P INPUT ACCEPT
/sbin/iptables -P OUTPUT ACCEPT
/sbin/iptables -P FORWARD ACCEPT
/sbin/iptables -t nat -P PREROUTING ACCEPT
/sbin/iptables -t nat -P POSTROUTING ACCEPT
/sbin/iptables -t nat -P OUTPUT ACCEPT
### test 172.16.3.102 ###
/sbin/iptables -t nat -A PREROUTING -d 电信IP -i eth0 -j DNAT --to-destination 172.16.3.102
/sbin/iptables -t nat -A PREROUTING -d 网通IP-i eth1 -j DNAT --to-destination 172.16.3.102
#out
/sbin/iptables -t nat -A POSTROUTING -s 172.16.3.102 -o eth0 -j SNAT --to 电信IP
/sbin/iptables -t nat -A POSTROUTING -s 172.16.3.102 -o eth1 -j SNAT --to 网通IP
### test end ###
### NAT ###
/sbin/iptables -t nat -A POSTROUTING -s 172.16.3.0/255.255.255.0 -j MASQUERADE
~
以上三种方法,供大家参考.