Last active
September 13, 2016 18:20
-
-
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.
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 | |
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