jablonka.czprosek.czf

sedlo

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

Compare with Previous - Blame - Download


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

sl_version="0.0.2"

PATH=$PATH:$HOME/bin:/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_fillrules()
{
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
      sl_empty="no"
      sl_cnt=3
#removing until we have some rules in table but the maximum of cycles is limited
#to avoid neverending loop
      while [ "$sl_empty" = "no" ]
      do 
        sl_ipru=`$sl_ipcmd ru ls | $sl_grepcmd $sl_ipn`
        if [ "$sl_ipru x" != " x" ]
          then
            if [ $scm_info -gt 1 ]; then echo "Removing old rules for $sl_ipn" ; fi
            $sl_ipcmd ru del from $sl_ipn
            $sl_ipcmd ru del from $sl_ipn to $sl_ipnodef 
          else
            sl_empty="yes"
        fi
        sl_cnt=$(($sl_cnt - 1 ))
        if [ "$sl_cnt" -eq -1 ]
        then
          sl_empty="yes"
          echo "Too much cycles"
        fi
      done
      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_ipcmd ru add from $sl_ipn to $sl_ipnodef lookup main
      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

} # 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}'`
    if [ "$sl_igwgt x" = " x" ]
      then
        if [ $scm_info -gt 1 ]; then echo "Route not found for igw $sl_igwn - leaving table empty" ; fi
        sl_tbl=`$sl_ipcmd ro ls ta $sl_igwn`
        if [ "$sl_tbl x" != " x" ]
          then
            $sl_ipcmd ro fl ta $sl_igwn 
        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

} # 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 $sl_murlcfg -O "$sl_sedlocache/$sl_nmcnf.main" -q
if [ -s $sl_sedlocache/$sl_nmcnf.main ]
  then 
    sl_nop=1  
  else 
    if [ $scm_info -gt 1 ]; then echo "Main config not found $sl_sedlocache/$sl_nmcnf.main" ; fi
fi
echo "# generated file" > $sl_sedlocache/$sl_nmcnf
for sl_file in `ls $sl_sedlocnf $sl_sedlocache/$sl_nmcnf.main `
do
cat $sl_file | grep "^mcnf" | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf
cat $sl_file | grep "^igw" | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
cat $sl_file | grep "^ip" | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}' >> $sl_sedlocache/$sl_nmcnf
done
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
  exit 0
} # s_version
######################################################################
s_report()
{
  echo "##### SEDLO #####"
  echo "version      : $sl_version"
  echo "local_config : $sl_sedlocnf"
  echo "main_config  : $sl_murlcfg"
  echo "##### TABLES #####"
  cat $sl_rttab
  echo "##### RULES #####"
  $sl_ipcmd ru ls
  exit 0
} # s_report
######################################################################
s_help()
{
  echo Pouziti: sedlo [param]
  echo param:
  echo -v vypise verzi
  echo -help vypise napovedu
  echo -info1 malo upovidany
  echo -info2 hodne upovidany
  echo -nogetcfg zajisti ze se nedude znovu nacitat konfigurace a pouzije se predchozi z cache
  echo -report vypise prehled pravidel a tabulek
  exit 0
} # s_help
######################################################################
######################################################################

sl_unknown=""
scm_nogetcfg=0
scm_info=0

# parsing input parameters
while [ "a$1" != "a" ]
do
  case $1 in
  -v)
     s_version
  ;;
  -h)
     s_help
  ;;
  -report)
     s_report
  ;;
  -help)
     s_help
  ;;
  -nogetcfg)
    scm_nogetcfg=1
    shift
  ;;
  -info1)
    scm_info=1
    shift
  ;;
  -info2)
    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
fi

if [ $scm_nogetcfg -eq 0 ]
then
  s_getcfg      
fi

s_mktables
s_filltables
s_fillrules

exit 0


Powered by WebSVN 2.2.1