21 Oktober 2009

 

Ip rule rules!

#!/bin/bash

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
IP="/sbin/ip";

TODO=$1
ROUTERIP=$2
ETH=$3

DEFAULT_ETH="br0"
DEFAULT_TABLE="main"

RT_TABLES="/etc/iproute2/rt_tables"
RT_COUNTER="/etc/my-route/etc/rt_tables_counter"
CR_COUNTER="/etc/my-route/etc/ip_rule_counter"

ERR1="$ROUTERIP not found as Directory."
ERR2="$ROUTERIP Directory is empty."

if [ "$ETH" = "" ]; then ETH=$DEFAULT_ETH; fi

function rttable_check() {

        BELUM=`cat $RT_TABLES | grep $T`;
        N=`cat $RT_COUNTER`; N=$((1+${N}));
        if [ "$BELUM" == "" ]; then
                echo "$N $T" >> $RT_TABLES
                echo $N > $RT_COUNTER
        fi

        C=`cat $CR_COUNTER`;

        ip_rulez; C=$((1+${C})); echo $C > $CR_COUNTER

}

function ip_rulez() {
        echo -n "Flush: $T "
        $IP route flush table $T
        echo ""
        echo -n "Add into Rule : 1$N $T "
        $IP rule add prio 1$C table $T
        echo ""
}

function iproute_add() {

        T=`echo $rf| cut -d/ -f5`;

        rttable_check

        echo -n "$T: "
        while W=`line`
        do
        if [ "$W" != "" ]; then
                echo -n "$W "
                $IP route add $W via $ROUTERIP dev $ETH table $T
                echo ""
        fi
        done < $rf
        $IP route flush cache
        echo ""
}

function iproute_del() {

        T=`echo $rf| cut -d/ -f5`;
        echo -n "$T: "
        while W=`line`
        do
        if [ "$W" != "" ]; then
                echo -n "$W "
                $IP route del $W via $ROUTERIP dev $ETH table $T
                echo ""
        fi
        done < $rf
        $IP route flush cache
        echo ""

}

function syntax() {
       echo "Syntax: $0 [build|delete] router-ip device"
       exit 1
}

function start () {

        if [ "$ROUTERIP" == "" ]; then
                syntax
                exit 0
        fi

        if [ ! -d "/etc/my-route/$ROUTERIP" ]; then
                echo $ERR1
                exit 0
        fi

        case "$TODO" in

        build)
                for rf in /etc/my-route/$ROUTERIP/*
                do
                iproute_add
                done;
        ;;
        delete)
                for rf in /etc/my-route/$ROUTERIP/*
                do
                iproute_del
                done;
        ;;

        *)
                syntax

        ;;
        esac

}

start
exit 0

Label: , ,


Komentar:

Posting Komentar

Berlangganan Posting Komentar [Atom]





<< Beranda

Arsip

Januari 2009   Oktober 2009   November 2009   Maret 2010   Januari 2011   Februari 2011   Juli 2011   Juni 2012