05-24-2006
script to change default route when primary gateway dies
Hello all!
We have two fedora routers one at each site. What we are trying to do worked using freesco but not on fedora, i'm not very good at scripting and need a little guidence. Basicly what we are trying to do is if the primary line dies, the router will change it's routes to go through our t1 line. Here's the script and i'm getting an error saying "syntax error near unexpected token `{ `initial_routes() {"
######################################################
# File Name: san_gw_switch.sh
# Author : XXXXXXXXXXX
# Date : 2/21/2006
# Company : XXXXXXXXXXX
######################################################
#
# Script Parameters
#
PRIMARY="192.168.100.57"
SECONDARY="192.168.100.49"
PRIMARY_GW="192.168.100.42"
SECONDARY_GW="192.168.100.54"
email="helpdesk@########.com"
PINGOUTPUT="/scripts/logs/pingout.txt"
ROUTEOUTPUT="/scripts/logs/routeout.txt"
logfile="/scripts/logs/pinghost.log"
#logfile="/dev/null 2>&1"
sleepcount=3 # sleep 3 seconds
opt="p" # ping initial Primary GW
echo "`date` - Gateway monitoring started..." > ${logfile}
#
#
#######################################################
initial_routes() {
/sbin/route add -net 192.168.253.240 gw 170.231.56.232 netmask 255.255.255.240 dev eth2
/sbin/route add -net 192.168.2.0 gw 10.1.0.2 netmask 255.255.255.0 dev eth2
/sbin/route add -net 192.168.99.0 gw 10.1.1.62 netmask 255.255.255.224 dev eth2
/sbin/route add -net 2.0.160.0 gw 10.1.1.66 netmask 255.255.255.0 dev eth2
/sbin/route add -net 10.145.0.0 gw 192.168.100.42 netmask 255.255.0.0 dev eth1
/bin/route add -net 192.168.100.36 gw 192.168.100.54 netmask 255.255.255.252 dev eth3
/sbin/route add -net 192.168.100.48 gw 192.168.100.42 netmask 255.255.255.252 dev eth1
/sbin/ifconfig eth2:0 inet 170.231.56.1 netmask 255.255.255.0 broadcast 170.231.56.255
}
chg_route_primary() {
/sbin/route del 192.168.100.48
/sbin/route del 10.145.0.0
/sbin/route add -net 192.168.100.48 gw ${PRIMARY_GW} netmask 255.255.255.252 dev eth1
/sbin/route add -net 10.145.0.0 gw ${PRIMARY_GW} netmask 255.255.0.0 dev eth1
/sbin/route -n > ${ROUTEOUTPUT}
cat ${ROUTEOUTPUT}
}
chg_route_secondary() {
/sbin/route del 192.168.100.48
/sbin/route del 10.145.0.0
/sbin/route add -net 192.168.100.48 gw ${SECONDARY_GW} netmask 255.255.255.252 dev eth3
/sbin/route add -net 10.145.0.0 gw ${SECONDARY_GW} netmask 255.255.0.0 dev eth3
/sbin/route -n > ${ROUTEOUTPUT}
cat ${ROUTEOUTPUT}
}
run_primary() {
/bin/ping -c 3 -i 3 $PRIMARY > ${PINGOUTPUT}
if [ "`sed -n /100%/p ${PINGOUTPUT}`" ]; then
echo "`date`" >> ${logfile}
echo " - Primary GW failure on: ${PRIMARY}" >> ${logfile}
echo " - Switching to secondary GW on: ${SECONDARY}" >> ${logfile}
### /bin/mailx -s "Switched to Secondary IP Address ${SECONDARY}" ${email} < /bin/date
opt="s" # setting to Secondary IP Address
chg_route_secondary
else
echo "...Success - Primary GW active on: ${PRIMARY}" >> ${logfile}
fi
} # End of function run_primary
#
#######################################################
re_check_primary() {
/bin/ping -c 3 -i 3 $PRIMARY > ${PINGOUTPUT}
if [ "`sed -n /100%/p ${PINGOUTPUT}`" ]; then
opt="s"
else
echo "`date`" >> ${logfile}
echo " - Primary GW recheck successful, switching back to primary on: ${PRIMARY}" >> ${logfile}
opt="p"
chg_route_primary
fi
} # End of function re_check_primary
#
#######################################################
run_secondary() {
/bin/ping -c 3 -i 3 $SECONDARY > ${PINGOUTPUT}
if [ "`sed -n /100%/p ${PINGOUTPUT}`" ]; then
echo "`date` - OVERALL FAILURE - Primary on: ${PRIMARY} and Secondary on: ${SECONDARY} are not responding!!!" >> ${logfile}
### /bin/mailx -s "Failed to find Primary ${PRIMARY} and Secondary ${SECONDARY} IP Addresses" $email} < /bin/date
opt="f" # setting to fail-fail option
# reset route back to primary
else
echo "`date`" >> ${logfile}
echo " - Failover Success - Secondary GW active on: ${SECONDARY}" >> ${logfile}
re_check_primary
fi
} # End of function run_secondary
#
#######################################################
#######################################################
# Main Script
#######################################################
initial_routes
while [ $opt != n ];
do
case $opt in
p) echo "OPTION: P" >> ${logfile}
sleep ${sleepcount} # waits x seconds
echo "Running primary check."
run_primary;;
s) echo "OPTION: S" >> ${logfile}
sleep ${sleepcount} # waits x seconds
echo "Running secondary check."
run_secondary;;
f) echo "OPTION: FAIL - Cannot Access Primary ${PRIMARY} or Secondary ${SECONDARY}" >> ${logfile}
###/bin/mailx -s "Cannot Access Primary $PRIMARY} and Secondary ${SECONDARY} IP Addresses" ${email} < /bin/date
echo "All routes failed!!!"
run_secondary;;
esac
done
#
We had this working on two freesco boxes, but those became a little unstable. We were having issues with freesco seeing or detecting nics and keeping the virtual nic. If anyone can post a link or anything it would be greatly appreciated.
Kevin
9 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
:confused:
Hi:
I donīt know how to add route gateway and when restart the machine lose this route and put other.
In SCO ver 5.6 Enterprise write:
etc\route delete gateway
route add gateway 199.199.1.1.
so , netstat -rn to see if has change -> all ok
Restart -> !!!... (2 Replies)
Discussion started by: AlvaroD
2 Replies
2. Solaris
my actual default gatewas IP is:
default 192.168.11.254
and I need to change it to 192.168.11.2, I'm using Solaris 9, how can i do it? and how i can save it in case that the machine restart and have the new default gateway IP saved? (5 Replies)
Discussion started by: Lestat
5 Replies
3. UNIX for Advanced & Expert Users
When users login, they are directed to menu (aix script). The menu enables the user to choose an environment to work in. Each environment has a different group id. When a user chooses a menu option, I want to change his primary group to that specific environment's group id. Is this at all possible... (3 Replies)
Discussion started by: terrym
3 Replies
4. AIX
Just want to know if someone has a clue about what could be happening here:
I have an AIX box with four NICs in Ether channel connected to a Cisco Switch.
When I try to ping the default gateway, sometimes it works, but sometimes it doesn't work unless it has a defined static route.
Thanks... (1 Reply)
Discussion started by: witt
1 Replies
5. AIX
Hi
how to change the primary dump device in a vio server ?
$ ioslevel
2.2.0.11-FP-24 SP-01
$ oem_setup_env
# sysdumpdev -l
primary /dev/sysdumpnull
secondary /dev/hd6
copy directory /var/adm/ras
forced copy flag TRUE
always allow dump TRUE
dump... (1 Reply)
Discussion started by: newtoaixos
1 Replies
6. Red Hat
Hi..I have to change default gateway in my linux machine. Its OS is RHEL6.
I followed following steps.
1.run command ip route to check current default gateway
2.delete that gateway with command
route del default gw x.x.x.x eth2
3.Add new gatewy with command below
route add default gateway... (3 Replies)
Discussion started by: deljatt
3 Replies
7. Red Hat
Hi all,
i have a rhel 6.2 with a default gateway.
This server is an Intranet office with no internet access.
DNS are already configured and it's possible to resolve my target.
My goal is to link my application (apache forward) only to http://mytarget.yyy through a new gateway (That does... (0 Replies)
Discussion started by: kamose
0 Replies
8. Linux
Hi,
This is the routing table on a RHEL 7 server :
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.132.81.145 0.0.0.0 UG 100 0 0 eno49
10.132.81.144 0.0.0.0 255.255.255.240 U 100 0 0 eno49... (4 Replies)
Discussion started by: anaigini45
4 Replies
9. UNIX for Beginners Questions & Answers
Dear friends
I use RedHat 6.5, which sets the gateway in the configuration file / etc / sysconfig / network as GATEWAY = 192.168.1.26, and the gateway in the configuration file / etc / sysconfig / network-scripts / ifcfg-eth11 as GATEWAY = 192.168.1.256. The two gateways are different.... (6 Replies)
Discussion started by: tanpeng
6 Replies