-
-
Save xombiemp/3429751 to your computer and use it in GitHub Desktop.
Script to check Internet connectivity and fail-over route table to standby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?