Skip to content

Instantly share code, notes, and snippets.

@alexalouit
Created April 22, 2016 10:26
Show Gist options
  • Save alexalouit/15bc7c92fe1eec953e4a585f88779f79 to your computer and use it in GitHub Desktop.
Save alexalouit/15bc7c92fe1eec953e4a585f88779f79 to your computer and use it in GitHub Desktop.
Connection status checker with iptables-conditions for dynamic routing, load balancing and failover
#!/bin/sh
#
# cron ex: @reboot /usr/bin/nice -n 19 /usr/bin/ionice -c2 -n7 /root/scripts/100_up.sh >> /root/scripts/cron.log
while true ; do
# do ping with interface
if /bin/ping -qc 3 8.8.8.8 -I eth0.100 > /dev/null; then
if /bin/grep -q 0 /proc/net/nf_condition/100_up; then /bin/echo 1 > /proc/net/nf_condition/100_up; fi
else
# do ping with fwmark, and to another target
if /bin/ping -qc 3 8.8.4.4 -m 100 > /dev/null; then
if /bin/grep -q 0 /proc/net/nf_condition/100_up; then /bin/echo 1 > /proc/net/nf_condition/100_up; fi
else
if /bin/grep -q 1 /proc/net/nf_condition/100_up; then /bin/echo 0 > /proc/net/nf_condition/100_up; fi
fi
fi
sleep 3
done
@alexalouit
Copy link
Author

This example assumes you have a eth0.100 interface, which has reachable valid default route and reachable number 100 fwmark.

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