代碼: |
[root@linuxrouter root]# ip rule ls 0: from all lookup local 32766: from all lookup main 32767: from all lookup 253 [root@linuxrouter root]# |
代碼: |
#!/bin/bash # Name: cprt # This program copy the route from $1 route table to $2 route table, # exclude the default route entry. if [ -z "$1" -o -z "$2" ]; then echo $"usage: cprt <source_table> <dest_table>" exit 1 fi SOURCE=$1 DEST=$2 # Clear the destination route table echo $"Clearing route table $DEST ......" echo /sbin/ip route flush table $DEST # Inject routes from source to destination echo $"Injecting route from $SOURCE to $DEST ......" /sbin/ip route ls table $SOURCE | grep -v default > /tmp/route-tmp while read line; do /sbin/ip route add table $DEST $line done < "/tmp/route-tmp" |
代碼: |
#!/bin/bash # Name: ip-up.local # Created by lyking@CU # If the if-down is not completed, this script can‘t be excute. while [ -e /var/lock/subsys/if-down.$IFNAME ] ; do sleep 3 done # Creat a lock file to prevent the if-down from running on my turn touch /var/lock/subsys/if-up.$IFNAME # Determin device here # We should use IFNAME as the interface name.For some reason, the IFNAME maybe not # same as the LINKNAME. And the route table should associate with the IFNAME. For # some conveniency, I name the route table as "ppp0" and "ppp1". RT_TABLE=$IFNAME # Add or change static route here,including default route. # Check if a default is exist. RS="" ip route ls table $RT_TABLE | grep default RS=$? if [ $RS -eq 0 ] ; then ip route change default dev $IFNAME table $RT_TABLE else ip route add default dev $IFNAME table $RT_TABLE fi echo >> /var/log/ifchang.log echo "$0: $IFNAME going up at `date`." >> /var/log/ifchang.log echo "$0: $IFNAME got address: $IPLOCAL, peer address is $IPREMOTE." >> /var/log/ifchang.log echo "$0: Table $RT_TABLE default route change to `ip route ls table $RT_TABLE | grep default`." >> /var/log/ifchang.log # Refresh routing cache to activating the routing immediately. ip route flush cache # Add traffic control policy here tc qdisc add dev $IFNAME root handle 1: prio ## This *instantly* creates classes 1:1, 1:2, 1:3 tc qdisc add dev $IFNAME parent 1:1 handle 10 sfq perturb 20 tc qdisc add dev $IFNAME parent 1:2 handle 20 sfq perturb 20 tc qdisc add dev $IFNAME parent 1:3 handle 30 sfq # Remove the lock file rm -f /var/lock/subsys/if-up.$IFNAME |
代碼: |
#!/bin/bash # Name: ip-down.local # Created by lyking@CU while [ -e /var/lock/subsys/if-up.$IFNAME ] ; do sleep 3 done touch /var/lock/subsys/if-down.$IFNAME cd /etc/sysconfig/network-scripts . network-functions # Determin device here # We should use IFNAME as the interface name.For some reason, the IFNAME maybe not # same as the LINKNAME. And the route table should associate with the IFNAME. For # some conveniency, I name the route table as "ppp0" and "ppp1". RT_TABLE=$IFNAME # Looking for a valide connection to Internet DEFAULT_RT="" PPPS=‘ppp0 ppp1‘ for i in $PPPS ; do ifconfig | grep $i RS=$? if [ $RS -eq 0 ] ; then DEFAULT_RT=$i break fi done # Update default route here as nesessary if [ $DEFAULT_RT != $IFNAME ] ; then if [ $DEFAULT_RT != "" ] ; then ip route add default dev $DEFAULT_RT table $RT_TABLE else for i in $PPPS ; do ip route del default dev $i table $i done echo >> /var/log/ifchang.log echo "$0: All connection is down, remove all default route from all branch tables" >> /var/log/ifchang.log fi echo >> /var/log/ifchang.log echo "$0: $IFNAME going down at `date`." >> /var/log/ifchang.log echo "$0: Connection lasted $CONNECT_TIME seconds." >> /var/log/ifchang.log echo "$0: $BYTES_SENT bytes sent, $BYTES_RCVD bytes received." >> /var/log/ifchang.log echo "$0: $DEFAULT_RT is available." >> /var/log/ifchang.log echo "$0: Table $RT_TABLE default route changed to `ip route ls table $RT_TABLE | grep default`. " >> /var/log/ifchang.log fi # Refresh routing cache to activating the routing immediately. ip route flush cache rm -f /var/lock/subsys/if-down.$IFNAME |
代碼: |
# Divid traffic to different mark iptables -t mangle -A PREROUTING -s 10.0.0.0/255.255.255.1 -j MARK --set-mark 0x1 iptables -t mangle -A PREROUTING -s 10.0.0.1/255.255.255.1 -j MARK --set-mark 0x2 # NAT /sbin/iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE /sbin/iptables -t nat -A POSTROUTING -o ppp1 -j MASQUERADE |
代碼: |
ppp0 Link encap:Point-to-Point Protocol inet addr:220.163.38.208 P-t-P:220.163.38.1 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1 RX packets:100295 errors:0 dropped:0 overruns:0 frame:0 TX packets:67817 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:108844271 (103.8 Mb) TX bytes:6073206 (5.7 Mb) ppp1 Link encap:Point-to-Point Protocol inet addr:220.163.36.57 P-t-P:220.163.36.1 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1 RX packets:150583 errors:0 dropped:0 overruns:0 frame:0 TX packets:125136 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:132921157 (126.7 Mb) TX bytes:8749585 (8.3 Mb) |
代碼: |
![]() | |