jablonka.czprosek.czf

sedlo

Subversion Repositories:
[/] [trunk/] [sedlo] - Rev 8 Go to most recent revision

Compare with Previous - Blame - Download


#!/bin/bash
# author       : Petr Simandl www.simandl.cz
# release date : 07/09/2004 
# name         : sedlo
# description  : dynamic side routing tables tool
# license      : GPL

#changelog
# - ip ro fl ca
# - myigw 

sl_version="0.0.3pre7"

PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

sl_nmcnf="sedlo.conf"
sl_sedlocnf="/etc/$sl_nmcnf"
sl_sedlocache="/var/cache/sedlo"

sl_rttab="/etc/iproute2/rt_tables"
sl_rtnmin=110
sl_rtnmax=200
sl_ipnodef="10.0.0.0/8"

slm_unknown="Nezname parametry : "

sl_ipcmd=`which ip`
sl_trcmd=`which tr`
sl_wgetcmd=`which wget`
sl_hnmcmd=`which hostname`
sl_awkcmd=`which awk`
sl_catcmd=`which cat`
sl_grepcmd=`which grep`

if [ -e $sl_sedlocnf ]
  then 
    sl_nop=1  
  else 
  echo "$sl_sedlocnf not found"
  exit 1
fi

if [ -e $sl_rttab ]
  then 
    sl_nop=1  
  else 
  echo "$sl_rttab not found"
  exit 1
fi

sl_murlcfg=`cat $sl_sedlocnf | grep "^mcnf" | uniq | awk '{print $2}'`

######################################################################
s_maru()
{
if [ $scm_info -gt 0 ]; then echo "Managing rules" ; fi

sl_rules=`($sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_awkcmd '{print $3"*ru"}' ; \
$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*ip"}') | sort | uniq`

sl_merged=`echo $sl_rules $sl_ips | sort | uniq`

echo $sl_merged

} # s_maru
######################################################################
s_flru()
{
if [ $scm_info -gt 0 ]; then echo "Flushing all rules" ; fi

sl_rules=`$sl_ipcmd ru ls | $sl_grepcmd -v "from all lookup" | $sl_grepcmd " 10." | $sl_trcmd '[:blank:]' '*'`

for sl_rule in $sl_rules 
do
#  sl_ipn=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2}'`
  sl_ipgws=`echo $sl_rule | $sl_awkcmd -F '*' '{print $2,$3,$4,$5,$6,$7}'`
  $sl_ipcmd ru del $sl_ipgws
done

#this should make faster applying of new routing tables
$sl_ipcmd ro flush cache

} # s_flru

######################################################################
s_fillrules()
{

#flush old rules before filling new ones
#not so nice solution - it is planned to change just what's necessary
#by s_maru
s_flru

if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi

sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'`

for sl_ip in $sl_ips 
do
  sl_ipn=`echo $sl_ip | $sl_awkcmd -F '*' '{print $1}'`
  sl_ipgws=`echo $sl_ip | $sl_awkcmd -F '*' '{print $2,$3,$4}'`
  sl_ok="no"
  for sl_ipgw in $sl_ipgws
  do
    sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw`
    if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ]
      then
        if [ $scm_info -gt 1 ]; then echo "Creating new rules to send $sl_ipn to table $sl_ipgw" ; fi
        $sl_ipcmd ru add from $sl_ipn lookup $sl_ipgw
        sl_ok="yes"
      else
        if [ "$sl_ok" = "no" ]
          then
            if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it is empty" ; fi
          else
          if [ $scm_info -gt 1 ]; then echo "For $sl_ipn table $sl_ipgw not used because it has lower priority" ; fi
        fi
    fi
  done
done

$sl_ipcmd ru add from $sl_ipnodef to $sl_ipnodef lookup main

#this should make faster applying of new routing tables
$sl_ipcmd ro flush cache

} # s_fillrules

######################################################################
s_filltables()
{
if [ $scm_info -gt 0 ]; then echo "Filling tables" ; fi

sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3"*"$2}'`

for sl_igw in $sl_igws 
  do
    sl_igwn=`echo $sl_igw | $sl_awkcmd -F '*' '{print $1}'`
    sl_igwip=`echo $sl_igw | $sl_awkcmd -F '*' '{print $2}'`
    sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd "^$sl_igwip " | $sl_awkcmd '{print $3}'`

    # equal cost multipath detection - just first is taken
    if [ "$sl_igwgt x" = "zebra x" ]
      then
        sl_igwgt=`$sl_ipcmd ro ls | $sl_grepcmd -A 1 "^$sl_igwip " | $sl_grepcmd "nexthop" | $sl_awkcmd '{print $3}'`
    fi

    if [ "$sl_igwgt x" = " x" ]
      then
        if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table as is" ; fi
      else
        sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
        if [ "$sl_tbl x" != " x" ]
          then
            $sl_ipcmd ro fl ta $sl_igwn 
        fi
        $sl_ipcmd ro add 0.0.0.0/1 via $sl_igwgt ta $sl_igwn 
        $sl_ipcmd ro add 128.0.0.0/1 via $sl_igwgt ta $sl_igwn 
        if [ $scm_info -gt 1 ]; then echo "Table filled for igw $sl_igwn" ; fi
    fi
  done

#this should make faster applying of new routing tables
$sl_ipcmd ro flush cache

} # s_filltables

######################################################################
s_mktables()
{
if [ $scm_info -gt 0 ]; then echo "Creating tables " ; fi
sl_igws=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^igw" | $sl_awkcmd '{print $3}'`
for sl_igw in $sl_igws 
  do
    sl_igwrttb=`$sl_catcmd $sl_rttab | $sl_awkcmd '{print $2}' | $sl_grepcmd $sl_igw ` 
    if [ "$sl_igwrttb x" = " x" ]
      then
        if [ $scm_info -gt 1 ]; then echo "Creating table for $sl_igw" ; fi
        sl_cnt="$sl_rtnmax"
        sl_ok="no"
        until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ]
        do
          #space is used to recognized two and three digit numbers
          sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` 
          if [ "$sl_igwrttb x" = " x" ]
            then
              sl_ok="yes"
              echo "$sl_cnt     $sl_igw" >> $sl_rttab 
          fi
          sl_cnt=$(($sl_cnt - 1 ))
        done  
      else
        if [ $scm_info -gt 1 ]; then echo "Table found for $sl_igw no action taken" ; fi
    fi

  done
} # s_mktables

######################################################################
s_getcfg()
{
if [ $scm_info -gt 0 ]; then echo "Getting config" ; fi
if [ $scm_info -gt 1 ]; then echo "Using main config $sl_murlcfg" ; fi
if [ $scm_info -gt 1 ]; then echo "Using local config $sl_sedlocnf" ; fi

wget -q -t 3 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main.tmp" 

if [ -s $sl_sedlocache/$sl_nmcnf.main.tmp ]
  then 
    date > $sl_sedlocache/last_getcnf.txt
    cp $sl_sedlocache/$sl_nmcnf.main.tmp $sl_sedlocache/$sl_nmcnf.main  
    if [ $scm_info -gt 1 ]; then echo "Main config accepted" ; fi
  else 
    if [ $scm_info -gt 1 ]; then echo "Main config not downloaded - cached config will be used" ; fi
    echo -n "Main config not downloaded " > $sl_sedlocache/last_getcnf.txt  
    date >> $sl_sedlocache/last_getcnf.txt  
fi

# preparing cached config from local and main
# the local config should be processed as the second to have
# higher priority for rules from local config

echo "# generated file" > $sl_sedlocache/$sl_nmcnf
for sl_file in `ls $sl_sedlocache/$sl_nmcnf.main ; ls $sl_sedlocnf`
do
cat $sl_file | grep "^mcnf" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
cat $sl_file | grep "^igw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
cat $sl_file | grep "^ip" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
done

#local gateways taken from local config
cat $sl_sedlocnf | grep "^myigw" | $sl_trcmd ';' '#' | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf

#cat $sl_sedlocache/$sl_nmcnf | sort | uniq  > $sl_sedlocache/$sl_nmcnf.uniq
#mv $sl_sedlocache/$sl_nmcnf.uniq $sl_sedlocache/$sl_nmcnf

}

######################################################################
s_version()
{
  echo sedlo $sl_version
} # s_version

######################################################################
s_report()
{
  echo "##### SEDLO #####"
  echo "date         : `date`"
  echo "version      : $sl_version"
  echo "local_config : $sl_sedlocnf"
  echo "main_config  : $sl_murlcfg"
  echo "last update  : `cat $sl_sedlocache/last_getcnf.txt`"
  echo "##### TABLES #####"
  cat $sl_rttab
  echo "##### RULES #####"
  $sl_ipcmd ru ls
} # s_report

######################################################################
s_help()
{
  echo Pouziti: sedlo [param]
  echo param:
  echo -V vypise verzi
  echo -help vypise napovedu
  echo -v malo upovidany
  echo -vv hodne upovidany
  echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache
  echo -report vypise prehled pravidel a tabulek
  echo -flru odstrani vsechny pravidla
} # s_help

######################################################################
######################################################################

sl_unknown=""
scm_nogetcfg=0
scm_flru=0
scm_info=0

# parsing input parameters
while [ "a$1" != "a" ]
do
  case $1 in
  -V)
    s_version
    exit 0
  ;;
  -h)
    s_help
    exit 0
  ;;
  -report)
    s_report
    exit 0
  ;;
  -flru)
    scm_flru=1
    shift
  ;;
  -help)
    s_help
    exit 0
  ;;
  -nogetcfg)
    scm_nogetcfg=1
    shift
  ;;
  -v)
    scm_info=1
    shift
  ;;
  -vv)
    scm_info=2
    shift
  ;;
  *)
    sl_unknown="$sl_unknown$1 "
    shift
  esac
done

# printing the list of bad parameters (if there are some)
if [ "a$sl_unknown" != "a" ]
then
  echo "$slm_unknown $sl_unknown"
  s_help
  exit 0
fi

if [ $scm_flru -eq 1 ]
then
  s_flru      
  exit 0
fi

if [ $scm_nogetcfg -eq 0 ]
then
  s_getcfg      
fi

s_mktables
s_filltables
s_fillrules

exit 0



Powered by WebSVN 2.2.1