整個網(wǎng)絡(luò)上的數(shù)據(jù)包轉(zhuǎn)發(fā)都是由路由表決定的,。
路由表是由一系列靜態(tài)路由與動態(tài)路由的數(shù)據(jù)組成,。
Linux提供了一個"Advanced IP routing and network device configuration tools"路由套件iproute2
iproute2的常見命令ip,ro,ls (ip就是ip命令,ro就是route,ls 是list)
#ip ro ls 與route -n顯示的差不多
#增加默認路由ip ro add default via 192.168.0.1 table 200
(具體的路由表在/etc/iproute2/rt_tables中)
#ip ro ls table 200 顯示該路由表中的路由
(注:不能在同一個路由表中有相同的網(wǎng)關(guān))
解決“從哪個接口來,,就從哪個接口出”的問題,,這解決從外訪問的問題,(感覺不需要設(shè)置,,默認也能達到效果)
ip route flush table net2
ip route add default via 192.168.0.1 dev eth0 src 192.168.0.10 table net2
ip rule add from 192.168.0.10 table net2
ip route flush table net3
ip route add default via 192.168.1.1 dev eth1 src 192.168.1.10 table net3
ip rule add from 192.168.1.10 table net3
設(shè)置默認網(wǎng)關(guān)
route add default gw 192.168.1.1
實例配置的腳本
IP0=
GW0=
NET0=
DEV0=eth0
IP1=
GW1=
NET1=
DEV1=eth1
if ! [ grep "220 cernet" /etc/iproute2/rt_tables > /dev/null 2>&1 ]; then echo "220 cernet" >> /etc/iproute2/rt_tables ; fi
if ! [ grep "230 ctc" /etc/iproute2/rt_tables > /dev/null 2>&1 ]; then echo "230 ctc" >> /etc/iproute2/rt_tables ; fi
ip route flush table cernet
ip route add default via ${GW0} dev ${DEV0} src ${IP0} table cernet
ip rule add from ${IP0} table cernet
ip route flush table ctc
ip route add default via ${GW1} dev ${DEV1} src ${IP1} table ctc
ip rule add from ${IP1} table ctc
GW0=
NET0=
DEV0=eth0
IP1=
GW1=
NET1=
DEV1=eth1
if ! [ grep "220 cernet" /etc/iproute2/rt_tables > /dev/null 2>&1 ]; then echo "220 cernet" >> /etc/iproute2/rt_tables ; fi
if ! [ grep "230 ctc" /etc/iproute2/rt_tables > /dev/null 2>&1 ]; then echo "230 ctc" >> /etc/iproute2/rt_tables ; fi
ip route flush table cernet
ip route add default via ${GW0} dev ${DEV0} src ${IP0} table cernet
ip rule add from ${IP0} table cernet
ip route flush table ctc
ip route add default via ${GW1} dev ${DEV1} src ${IP1} table ctc
ip rule add from ${IP1} table ctc
以上解決了進來的路由,,大多服務(wù)器是提供服務(wù)的,所以有了從哪里來回哪里去就解決了所有的問題,。
如果是郵件服務(wù)器或自己上網(wǎng),,需要主動向外請求這樣還不夠,因為出去的包只會走默認路由,,
目標(biāo)就是: 請求的目的主機在電信讓它走電信的接入線路,、請求的教育網(wǎng)主機讓它走教育網(wǎng)線路、請求的主機在網(wǎng)通讓它走網(wǎng)通的接入線路....
似乎沒有更好的辦法,,只有根據(jù)IP段預(yù)先設(shè)置好路由規(guī)則
實現(xiàn)這個目標(biāo)就很簡單了,,找到各運營商的IP段
教育網(wǎng)的就簡單了https://www.nic.edu.cn/RS/ipstat/cernet-ipv4.txt 上面全有了...
(不能wget,只能自己拷貝,郁悶)
變成命令行加在上面的腳本中:
(以vim內(nèi)可以參考命令%s/^/ip rule add to /g與:%s/$/ table cernet/g)
ip rule add to 58.154.0.0/15 table cernet
ip rule add to 58.192.0.0/12 table cernet
....
將默認路由設(shè)為電信網(wǎng)接入的網(wǎng)關(guān),,這樣解決了出去的路由,。
同樣的辦法可以解決三網(wǎng)接入的問題