jablonka.czprosek.czf

sedlo

Subversion Repositories:
[/] [trunk/] [sedlo] - Diff between revs 2 and 3

Show entire file Ignore whitespace

Rev 2 Rev 3
Line 1... Line 1...
#!/bin/bash #!/bin/bash
# author : Petr Simandl www.simandl.cz # author : Petr Simandl www.simandl.cz
# release date : 19/08/2004 # release date : 29/08/2004
# name : sedlo # name : sedlo
# description : dynamic side routing tables tool # description : dynamic side routing tables tool
# license : GPL # license : GPL
   
sl_version="0.0.1" sl_version="0.0.2"
   
PATH=$PATH:$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin PATH=$PATH:$HOME/bin:/bin:/sbin:/usr/bin:/usr/sbin:usr/local/bin:/usr/local/sbin
   
sl_nmcnf="sedlo.conf" sl_nmcnf="sedlo.conf"
sl_sedlocnf="/etc/$sl_nmcnf" sl_sedlocnf="/etc/$sl_nmcnf"
sl_sedlocache="/var/cache/sedlo" sl_sedlocache="/var/cache/sedlo"
   
sl_rttab="/etc/iproute2/rt_tables" sl_rttab="/etc/iproute2/rt_tables"
sl_rtnmin=110 sl_rtnmin=110
sl_rtnmax=125 sl_rtnmax=200
  sl_ipnodef="10.0.0.0/8"
   
sl_ipcmd=`which wget` slm_unknown="Nezname parametry : "
   
  sl_ipcmd=`which ip`
  sl_trcmd=`which tr`
  sl_wgetcmd=`which wget`
sl_hnmcmd=`which hostname` sl_hnmcmd=`which hostname`
sl_awkcmd=`which awk` sl_awkcmd=`which awk`
  sl_catcmd=`which cat`
  sl_grepcmd=`which grep`
sl_time=`date '+%Y%m%d%H%M%S'`  
   
if [ -e $sl_sedlocnf ] if [ -e $sl_sedlocnf ]
then then
sl_nop=1 sl_nop=1
else else
Line 41... Line 45...
fi fi
   
sl_murlcfg=`cat $sl_sedlocnf | grep mcnf | uniq | awk '{print $2}'` sl_murlcfg=`cat $sl_sedlocnf | grep mcnf | uniq | awk '{print $2}'`
   
###################################################################### ######################################################################
s_mktables() s_fillrules()
{ {
echo s_mktables if [ $scm_info -gt 0 ]; then echo "Creating rules" ; fi
   
sl_igws=`cat $sl_sedlocache/$sl_nmcnf | grep "^igw" | awk '{print $3}'` sl_ips=`$sl_catcmd $sl_sedlocache/$sl_nmcnf | $sl_grepcmd "^ip" | $sl_awkcmd '{print $2"*"$4"*"$5"*"$6}'`
   
for sl_igw in $sl_igws 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 do
sl_igwrttb=`cat $sl_rttab | awk '{print $2}' | grep $sl_igw ` sl_tbl=`$sl_ipcmd ro ls ta $sl_ipgw`
if [ "$sl_igwrttb x" = " x" ] if [ "$sl_tbl x" != " x" ] && [ "$sl_ok" = "no" ]
then then
echo "Table for $sl_igw not found - creating" sl_empty="no"
sl_cnt="$sl_rtnmax" sl_cnt=3
sl_ok="no" #removing until we have some rules in table but the maximum of cycles is limited
until [ "$sl_cnt" -eq "$sl_rtnmin" ] || [ "$sl_ok" = "yes" ] #to avoid neverending loop
do while [ "$sl_empty" = "no" ]
#space is used to recognized two and three digit numbers do
sl_igwrttb=`cat $sl_rttab | awk '{print $1" "}' | grep "$sl_cnt " ` sl_ipru=`$sl_ipcmd ru ls | $sl_grepcmd $sl_ipn`
if [ "$sl_igwrttb x" = " x" ] if [ "$sl_ipru x" != " x" ]
then then
sl_ok="yes" if [ $scm_info -gt 1 ]; then echo "Removing old rules for $sl_ipn" ; fi
echo "$sl_cnt $sl_igw" >> $sl_rttab $sl_ipcmd ru del from $sl_ipn
  $sl_ipcmd ru del from $sl_ipn to $sl_ipnodef
  else
  sl_empty="yes"
fi fi
sl_cnt=$(($sl_cnt - 1 )) sl_cnt=$(($sl_cnt - 1 ))
done 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 else
echo "Table for $sl_igw found" 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 fi
   
done done
} } # s_mktables
###################################################################### ######################################################################
s_getcfg() s_getcfg()
{ {
echo 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 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 echo "# generated file" > $sl_sedlocache/$sl_nmcnf
for sl_file in `ls $sl_sedlocnf $sl_sedlocache/$sl_nmcnf.main ` for sl_file in `ls $sl_sedlocnf $sl_sedlocache/$sl_nmcnf.main `
do do
cat $sl_file | grep "^mcnf" | awk '{print $1"\t"$2}' >> $sl_sedlocache/$sl_nmcnf 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 "^igw" | awk '{print $1"\t"$2"\t"$3}' >> $sl_sedlocache/$sl_nmcnf
Line 94... Line 191...
###################################################################### ######################################################################
s_version() s_version()
{ {
echo sedlo $sl_version echo sedlo $sl_version
exit 0 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() s_help()
{ {
echo Pouziti: sedlo [param] echo Pouziti: sedlo [param]
echo param: echo param:
echo -v vypise verzi 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 exit 0
} } # s_help
###################################################################### ######################################################################
###################################################################### ######################################################################
   
s_getcfg 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_mktables
  s_filltables
  s_fillrules
   
exit 0 exit 0
   
   

Powered by WebSVN 2.2.1