[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash script fails in squeeze



On Wed, Jul 6, 2011 at 4:33 PM, Bonno Bloksma <b.bloksma@tio.nl> wrote:
> Hi,
>
> This script has been working flawlesly in Debian Etch and Lenny but after
> upgrading to Squeeze it no longer seems to work and I cannot find out why.
>
> Below the full "defaultgw-test.sh" script which attempts to determin whethet
> the local internet gateway (ADLS, Cable, etc) is functioning. If not it will
> switch the default gateway over to the intranet connection where the
> internet traffic when then leave our WAN network. This way each site has a
> fast local consumer internet gateway but still has the reliability of a
> buisiness connection.
>
> This bash script has been doing it's job for the past few years but suddenly
> stopped working in squeeze. Each part seems to work but the complete script
> does not. :-(
> I have been doing some test runs to see where the problem is and I cannot
> find it. I hope someone can shed some light on this weird problem.
>
> In the /etc/tiofirewall.conf file is a line
> UPLINK_IP=192.168.178.1
> which the script reads to determine what the default gateway for that site
> is supposed to be.
>
> ----------<defaultgw-test.sh>-----------------------------
> #!/bin/bash
>
> # v1.2
> # 12-jul-2010
> # 1.0 PINGHOST2 nu ook goed toegevoegd voor test defaultgw in de lucht
> # pingtest() nu met count 2
> # 1.1 ADSL_IP heet nu UPLINK_IP (change firewall 3.43)
> # Debug info toegevoegd
> # 1.2 IP nummer fw.tio.nl gewijzigd
>
> DEBUGINFO=false
> # PINGHOST is ping.xs4all.nl, de maatstaf of de DSL het nog doet
> PINGHOST=194.109.21.51
> # PINGHOST2 is ip van fw.tio.nl voor het geval de eerste even niet
> bereikbaar is
> PINGHOST2=77.222.72.50
> # DEFAULTGW moet de defaultgateway van deze machine zijn
> DEFAULTGW=`grep UPLINK_IP= /etc/tiofirewall.conf | cut -d= -f2`
>
> if [ $DEBUGINFO = true ] ; then
>  echo "Default gateway is" $DEFAULTGW
> fi
>
> # hieronder hoeft niets gewijzigd te worden
>
> function pingtest() {
>  IPHOST=$1
>  ping -c2 $IPHOST >/dev/null 2>&1
>  VALUE=$?
>  [ $VALUE == 0 ] && echo OK || echo NOK
> }
>
> function routezebra() {
>  [ X"`ip route ls | grep ^default | grep zebra`" == X ] && \
>   echo NO || echo YES
> }
>
> function nodefaultgw() {
>  [ `routezebra` == NO ] && \
>   echo "deleting default route via DSL, now learning default via OSPF!" && \
>   ip route del default
> }
>
> function yesdefaultgw() {
>  [ `routezebra` == YES ] && \
>   echo "adding default route via DSL, ignoring default learnt via OSPF" && \
>   ip route add default via $DEFAULTGW
> }
>
> # test of $PINGHOST pingt
> # pingt hij niet, test dan nog een keer
> # pingt hij dan nog niet, verwijder dan de defaultroute
> [ `pingtest $PINGHOST` == NOK ] && \
> [ `pingtest $PINGHOST2` == NOK ] && \
>  sleep 2 && \
>  [ `pingtest $PINGHOST` == NOK ] && \
>  [ `pingtest $PINGHOST2` == NOK ] && \
>  nodefaultgw
>
> sleep 2
>
> # test of $PINGHOST pingt
> # pingt hij, test dan nog een keer
> # pingt hij dan weer, voeg dan de defaultroute toe
> ([ `pingtest $PINGHOST` == OK ] || [ `pingtest $PINGHOST2` == OK ]) && \
>  sleep 2 && \
>  ([ `pingtest $PINGHOST` == OK ] || [ `pingtest $PINGHOST2` == OK ]) && \
>  yesdefaultgw
> ----------<\defaultgw-test.sh>-----------------------------
>
>
> Below some tests.
> ----------<callscript.sh>-----------------------------
> echo pingonly.sh ----------
> /root/test/pingonly.sh
> echo status=$?
>
> echo ping2.sh ----------
> /root/test/ping2.sh
> echo status=$?
>
> echo ping3.sh ----------
> /root/test/ping3.sh
> echo status=$?
>
> echo ping4.sh ----------
> /root/test/ping4.sh
> echo status=$?
>
> echo no-yes-defaultgw.sh ----------
> /root/test/no-yes-defaultgw.sh
> echo status=$?
> ----------<\callscript.sh>-----------------------------
>
> OUTPUT callscript
> =================
> linrtm:~/test# ./callscript.sh
> pingonly.sh ----------
> OK
> OK
> status=0
> ping2.sh ----------
> default gateway
> status=0
> ping3.sh ----------
> default gateway
> status=0
> ping4.sh ----------
> status=1
> no-yes-defaultgw.sh ----------
> deleting default route via DSL, now learning default via OSPF!
> adding default route via DSL, ignoring default learnt via OSPF
> status=0
>
> SCRIPT INHOUD
> =============
>
> All scripts contain the same content as the defaultgw-test.sh script with
> the same procedures and definitions. Only the "action lines" at the end of
> the script are different.
>
> pingonly.sh ----------
> pingtest $PINGHOST
> pingtest $PINGHOST2
>
> ping2.sh ----------
> # test of $PINGHOST pingt
> # pingt hij niet, test dan nog een keer
> # pingt hij dan nog niet, verwijder dan de defaultroute
> [ `pingtest $PINGHOST` == NOK ] && \
> [ `pingtest $PINGHOST2` == NOK ] && \
>  echo no default gateway || echo default gateway
>
> ping3.sh ----------
> # test of $PINGHOST pingt
> # pingt hij niet, test dan nog een keer
> # pingt hij dan nog niet, verwijder dan de defaultroute
> [ `pingtest $PINGHOST` == NOK ] && \
> [ `pingtest $PINGHOST2` == NOK ] && \
>  sleep 2 && \
>  [ `pingtest $PINGHOST` == NOK ] && \
>  [ `pingtest $PINGHOST2` == NOK ] && \
>  echo no default gateway || echo default gateway
>
> ping4.sh ----------
> # test of $PINGHOST pingt
> # pingt hij niet, test dan nog een keer
> # pingt hij dan nog niet, verwijder dan de defaultroute
> [ `pingtest $PINGHOST` == NOK ] && \
> [ `pingtest $PINGHOST2` == NOK ] && \
>  sleep 2 && \
>  [ `pingtest $PINGHOST` == NOK ] && \
>  [ `pingtest $PINGHOST2` == NOK ] && \
>  nodefaultgw
>
>
> no-yes-defaultgw.sh ----------
> nodefaultgw
> sleep 2
> yesdefaultgw
>
>
> I hope someone can find a reason why for instance ping4 fails and ping3 does
> not.
Should be related of nodefaultgw function ... ping4 use it and ping3 nor

If nodefaultw returns not 0 is because:

a) ip route ls | grep ^default | grep -q zebra returns 0 (zebra is in
the default gateway)
b) ip route del default  fails and return 1

Execute by hand or as people told you use #!/bin/bash -x and look in the output.

On the other hand, I would recommend you using the next syntax (I
imagine your code is a legacy code):

ping4.sh:
if ! ping -c 2 $IP &> /dev/null &&
   ! ping -c 2 $IP2 &> /dev/null &&
   sleep 2 && ..
then
      nodefaultgw
fi

or simply function pingtest() { ping -c 2 $1 &> /dev/null } and then use
if !pingtest $IP && ! pingtest $IP2 && ...

Not need using [ "$(xxx)" == "XX" ]

Regards,


Reply to: