Quote:
Originally Posted by srinivasanece
Yeah You got it right.Thank You So much.
|
#!/bin/bash
ping -q -c1 192.168.28.11 > /dev/null
if [ $? -eq 0 ] ; then
echo "Route Ok"
else
echo "Changing Route"
GW=$(/sbin/route | grep default | awk -F. {'print $4'})|cut -d ' ' -f1 # Find out what the last octet of the current default route
echo $GW |cut -d ' ' -f1
if [ ${GW} -eq "100" ] ; then # If it's 100
GW="101" # we change it to 101
else # If it's not 100
GW="100" # we change it t0 100
fi
/sbin/route del default
/sbin/route add default gw 192.168.27.${GW}
fi
When i run the above script i get an error.
line 10: [: -eq: unary operator expected
---------- Post added 04-25-14 at 03:01 PM ----------
Quote:
Originally Posted by TenTenths
First put [code] tags around your scripts.
Code:
#!/bin/sh
ping -q -c5 yahoo.com > /dev/null
if [ $? -eq 0 ]
then
echo "Route Ok"
else
echo "Changing Route"
/sbin/route del default
/sbin/route add default gw 192.168.27.101
if [ $? -eq 0 ]
then
echo "Route Ok"
else
echo "Changing Route"
/sbin/route del default
/sbin/route add default gw 192.168.27.100
fi
fi
Secondly, your script will change the gateway to 101 and then if changing the gateway isn't successful it'll change it to 100, not what I think you want to achieve.
Do you have a preferred gateway or do you only want to switch gateways when there is a failure?
If you're only interested in switching when there's a failure you could do something like:
Code:
#!/bin/bash
ping -q -c5 yahoo.com > /dev/null
if [ $? -eq 0 ] ; then
echo "Route Ok"
else
echo "Changing Route"
GW=$(/sbin/route | grep default | awk -F. {'print $4'}) # Find out what the last octet of the current default route
if [ ${GW} -eq "100" ] ; then # If it's 100
GW="101" # we change it to 101
else # If it's not 100
GW="100" # we change it t0 100
fi
/sbin/route del default
/sbin/route add default gw 192.168.27.${GW}
fi
You may have to tweak the code to get it to run right as I've not tested it but it should be an idea to get you in the right direction.
|
#!/bin/bash
ping -q -c1 192.168.28.11 > /dev/null
if [ $? -eq 0 ] ; then
echo "Route Ok"
else
echo "Changing Route"
GW=$(/sbin/route | grep default | awk -F. {'print $4'})|cut -d ' ' -f1 # Find out what the last octet of the current default route
echo $GW |cut -d ' ' -f1
if [ ${GW} -eq "100" ] ; then # If it's 100
GW="101" # we change it to 101
else # If it's not 100
GW="100" # we change it t0 100
fi
/sbin/route del default
/sbin/route add default gw 192.168.27.${GW}
fi
When i run the above script i get an error.
line 10: [: -eq: unary operator expected