Skip to content

Instantly share code, notes, and snippets.

@j3tm0t0
Created August 14, 2012 17:57
Show Gist options
  • Save j3tm0t0/3351281 to your computer and use it in GitHub Desktop.
Save j3tm0t0/3351281 to your computer and use it in GitHub Desktop.
Script to check Internet connectivity and fail-over route table to standby
#!/bin/sh
. /etc/profile.d/aws-apitools-common.sh
region=ap-northeast-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
subnetid=subnet-PRIVATE2
# 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
# check current route table and association
eval `ec2-describe-route-tables --region ap-northeast-1 -F association.subnet-id=$subnetid | egrep '^(ROUTETABLE|ASSOCIATION)' | awk '{print $1"="$2}'`
if ping -c 4 $target2 > /dev/null
then
if [ "$ROUTETABLE" != "$active_rt" ]
then
echo could ping $target 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 $target 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
@xombiemp
Copy link

I can't figure out how it works. What should go in the target1 and target2 variables? If the script is running on a NAT instance, how can the script ping "via" the other NAT instance?

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