Posted in Linux

iproute2

iproute2 Posted on 2006 年 11 月 14 日2 Comments

EX1

echo "200 cross" >> /etc/iproute2/rt_tables
ip rule add from [來源ip] table cross
ip route add default via [對外ip] dev eth0 table cross
ip route flush cache


EX2

 ——–
 |        |+ 1.1.1.1/24 — 1.1.1.1.254 —–>
 |        |                                                     [ Internet ]
 |        |+ 2.2.2.2/24 — 2.2.2.2.254 —–>
 ——–

ip addr add 1.1.1.1/24 dev eth0
ip addr add 2.2.2.2/24 dev eth0

ip rule add from 1.1.1.1 lookup 201
ip route add default via 1.1.1.254 dev eth0 table 201

ip rule add from 2.2.2.2 lookup 202
ip route add default via 2.2.2.254 dev eth1 table 202

ip route replace default equalize nexthop via 1.1.1.254 dev eth0 nexthop via 2.2.2.254 dev eth0

ip route flush cache

ref: http://www.study-area.net/tips/multipath.txt


EX3

                                    ———-
                                    |          |+ eth1 1.1.1.1 —> 1.1.1.253 —–>
 [ 10.0.0.0/24 ] eth0  +|          |                                                       [ Internet ]
                                    |          |+ eth2 2.2.2.2 —> 2.2.2.253 —–>
                                    ———-

ip rule add pref 10 to 10.0.0.0/24 table 10
ip route add 10.0.0.0/24 table 10 dev eth1
 
ip rule add pref 20 from 1.1.1.1 table 20
ip route add default table 20 via 1.1.1.253
 
ip rule add pref 20 from 2.2.2.2 table 30
ip route add default table 30 via 2.2.2.253

ip ruote replace default nexthop via 1.1.1.253 dev eth1 weight 1 nexthop via 2.2.2.253 dev eth2 weight 3

ip route flush cache

ref: http://www.study-area.net/tips/m_routing.htm

EX4
                     ———-
                     |          | eth0 [ 220.130.96.21 ]+———————————————+[ 220.130.96.254 ]
    [ .3 ] eth2 |          |
                     |          | eth1 [ 192.168.100.2 ]+—-+ ip share [ 192.168.100.1 ]—-
                     ———-    |                                                                                        |—–+[ 210.64.33.1 ]
                                     + ppp0 [ 210.64.33.27 ]+—————————————-

ip rule add pref 10 from 210.130.96.21 table 10
ip rule add pref 20 from 192.168.100.2 table 20
ip rule add pref 30 from 210.64.33.27 table 30

ip route replace default via 220.130.96.254 dev eth0 table 10
ip route replace default via 192.168.100.1 dev eth1 table 20
ip route replace default via 210.64.33.1 dev ppp0 table 30

ip route replace default nexthop via 220.130.96.254 dev eth0 weight 4  nexthop via 192.168.100.1 dev eth1 weight 1 nexthop via 210.64.33.1 dev ppp0 weight 1

ip route flush cache

ref: http://www.study-area.net/tips/multipath.htm

EX5
                     ———-
                     |          |+ eth1 [ 63.89.102.157 ] —> [ 63.89.102.1 ]—–>
 [ .1 ] eth0  +|          |                                                                               [ Internet ]
                     |          |+ eth2 [ 61.3.17.133 ]     —> [ 61.3.17.1 ]——–>
                     ———-

ip rule add from 63.89.102.157 table 10
ip route add default via 63.89.102.1 table 10
ip route add 10.0.0.0/24 via 10.0.0.1 table 10

ip rule add from 63.3.17.133 table 20
ip route add default via 63.3.17.1 table 20
ip route add 10.0.0.0/24 via 10.0.0.1 table 20

ip route add default equalize nexthop via 63.89.102.1 dev eth1 nexthop via 63.3.17.1 dev eth2

ip route flush cache

ref:
http://developer.ccidnet.com/art/295/20030324/41245_1.html

EX6
ref: http://phorum.study-area.org/viewtopic.php?t=41329&highlight=%E4%BA%94%E5%BC%B5%E7%B6%B2%E5%8D%A1

others
ref:
http://phorum.study-area.org/viewtopic.php?t=29573&highlight=%BA%F4%ACq
http://www.study-area.net/tips/2.4routing/2.4routing-big5-4.html

EX7
            ——-
            |      | ppp0 218.166.47.236 ————- 218.166.20.254 ————->
    eth0 |      |                                                                                                 [ Internet ]
            |      | eth1 61.63.33.173 —————– 61.63.33.174 —————->
            ——-

ip route replace default via 218.166.20.254 table 100
ip route replace default via 61.63.33.174 table 200
ip rule add from 218.166.47.236 table 100
ip rule add from 61.63.33.173 table 200
ip route replace default nexthop via 61.63.33.174 dev eth1 weight 1 nexthop via 218.166.20.254 dev ppp0 weight 1
ip route flush cache

ref: 旗X某某研究室

EX8 (CROSS實作)
環境: 同一ADSL模擬兩條線路
                                                             ——
                                                             |      | eth0 10.1.1.111 —————- 10.1.1.254 NAT———–
 172.16.1.123 —– 172.16.1.254 eth2 |      |                                                                                      |
                                                             |      | eth1 192.168.1.111                                                      | — [ Internet ]
                                                             ——-   |                                                                                  |
                                                                         + ppp0  59.117.50.104 ———- 218.160.156.254—–

ip rule add from 10.1.1.111         table 10 pref 10
ip rule add from 220.137.59.178 table 20 pref 20

ip route replace default via 10.1.1.254           dev eth0  table 10
ip route replace default via 218.160.156.254 dev ppp0 table 20

ip route replace default nexthop via 10.1.1.254 dev eth0  weight 1 nexthop via 218.160.156.254 dev ppp0 weight 1

ip route flush cache

測式traceroute
1. 每routing同一目的會一直使用同一線路
2. routing不同目的會跑不同線路

以下 script 尚在研究測式中,如何自動的、有效的判斷連線狀況

#!/bin/bash
set -x

EX_IF_1=eth0
EX_IF_2=ppp0

EX_IP_1=10.1.1.111
EX_IP_2=`ip route | grep $EX_IF_2 | awk '{print $9}' | head -n1`

EX_GW_1=10.1.1.254
EX_GW_2=`ip route | grep $EX_IF_2 | awk '{print $1}' | head -n1`

ip rule del table 10
ip rule del table 20

ip rule add from $EX_IP_1 table 10 pref 10
ip rule add from $EX_IP_2 table 20 pref 20

ip route replace default via $EX_GW_1 dev $EX_IF_1 table 10
ip route replace default via $EX_GW_2 dev $EX_IF_2 table 20

ip route replace default nexthop via $EX_GW_1 dev $EX_IF_1 weight 1 nexthop via $EX_GW_2 dev $EX_IF_2 weight 1

ip route flush cache

ping -c5 $EX_GW_1
if [ `echo $?` = 1 ]; then
        ip route replace default via $EX_GW_2 dev $EX_IF_2
        ip route flush cache
else
        ping -c5 $EX_GW_2
        if [ `echo $?` = 1 ]; then
                ip route replace default via $EX_GW_1 dev $EX_IF_1
                ip route flush cache
        else
                ip route replace default via $EX_GW_1 dev $EX_IF_1
                ip route flush cache
        fi
fi

2 comments

  1. 這…這樣可以拿來當router用咩?

    [Reply]

    cross Reply:

    可以當router,但重點在於如何讓它自動的、有效的判斷連線狀況。
    我在想市面上的硬體式的router有HA機制的,可能是用這個吧,我也不是很了,因為我沒用過硬體式有HA的router。

    [Reply]

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *