Skip to content

Instantly share code, notes, and snippets.

@joka90
Last active September 13, 2016 18:20
Show Gist options
  • Save joka90/6ee9c934c5f3bf7e477cbdcfa8d04f5c to your computer and use it in GitHub Desktop.
Save joka90/6ee9c934c5f3bf7e477cbdcfa8d04f5c to your computer and use it in GitHub Desktop.
Simple script to change firewallrules in DD-WRT to handle if the main server is not accessible. Switches back again when the main server becomes accessible again.
#!/bin/sh
MAIN_SERVER="192.168.1.8"
FAIL_OVER_SERVER="192.168.1.9"
PORT="80"
ACTIVE_SERVER=1
SAMPLING_TIME="5"
while :
do
while ping -c 1 $MAIN_SERVER &> /dev/null
do
echo "Accessed"
if [ "$ACTIVE_SERVER" -ne "1" ] ;
then
# MY enable firewall rule for server 2
echo "enable 1"
ACTIVE_SERVER=1
iptables -D -t nat -I PREROUTING -p tcp --dport $PORT -j DNAT --to $FAIL_OVER_SERVER
iptables -D -I FORWARD -p tcp -d $FAIL_OVER_SERVER --dport $PORT -j ACCEPT
iptables -A -t nat -I PREROUTING -p tcp --dport $PORT -j DNAT --to $MAIN_SERVER
iptables -A -I FORWARD -p tcp -d $MAIN_SERVER --dport $PORT -j ACCEPT
fi
sleep $SAMPLING_TIME # sleep 5 sec
done
while ! ping -c 1 $MAIN_SERVER &> /dev/null
do
echo "Not Accessed"
if [ "$ACTIVE_SERVER" -eq "1" ] ;
then
# MY enable firewall rule for server 2
echo "enable 2"
ACTIVE_SERVER=2
iptables -D -t nat -I PREROUTING -p tcp --dport $PORT -j DNAT --to $MAIN_SERVER
iptables -D -I FORWARD -p tcp -d $MAIN_SERVER --dport $PORT -j ACCEPT
iptables -A -t nat -I PREROUTING -p tcp --dport $PORT -j DNAT --to $FAIL_OVER_SERVER
iptables -A -I FORWARD -p tcp -d $FAIL_OVER_SERVER --dport $PORT -j ACCEPT
fi
sleep $SAMPLING_TIME # sleep 5 sec
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment