Skip to content

Instantly share code, notes, and snippets.

@xombiemp
Forked from j3tm0t0/check_and_failover_NAT.sh
Created August 22, 2012 21:57
Show Gist options
  • Save xombiemp/3429751 to your computer and use it in GitHub Desktop.
Save xombiemp/3429751 to your computer and use it in GitHub Desktop.
Script to check Internet connectivity and fail-over route table to standby
#!/bin/sh
. /home/ec2-user/.bash_profile
. /etc/profile.d/aws-apitools-common.sh
region=us-east-1
# these should be configured for each NAT instance
target1=8.8.8.8
target2=8.8.4.4
active_rt=rtb-ACTIVE
standby_rt=rtb-STANDBY
subnetids=(subnet-SUBNET1 subnet-SUBNET2)
# die with error if can't ping my target as I might have problem in Internet connectivity
ping -c 4 $target1 > /dev/null || exit -1
if ping -c 4 $target2 > /dev/null
then
pingable=true
else
pingable=false
fi
for subnetid in ${subnetids[*]}; do
# check current route table and association
eval `ec2-describe-route-tables --region $region -F association.subnet-id=$subnetid | egrep '^(ROUTETABLE|ASSOCIATION.*'$subnetid')' | awk '{print $1"="$2}'`
if $pingable
then
if [ "$ROUTETABLE" != "$active_rt" ]
then
echo could ping $target2 via NAT instance in opposite zone. reverting RouteTable to active.
echo ec2-replace-route-table-association --region $region -r $active_rt $ASSOCIATION
ec2-replace-route-table-association --region $region -r $active_rt $ASSOCIATION
fi
else
if [ "$ROUTETABLE" != "$standby_rt" ]
then
echo could NOT ping $target2 via NAT instance in opposite zone. swapping RouteTable to standby.
echo ec2-replace-route-table-association --region $region -r $standby_rt $ASSOCIATION
ec2-replace-route-table-association --region $region -r $standby_rt $ASSOCIATION
fi
fi
done
@addisonj
Copy link

can you give an example of what your network and route tables need to look like for this script to work?

Do you run this on each nat instance? or just the backup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment