#!/bin/sh # Default GW GW1=97.229.191.1 GW2=88.34.0.1 # IP for check dst1=8.8.8.8 dst2=8.8.4.4 dst3=4.2.2.1 #dst1=1.1.1.1 #dst2=2.2.2.2 #dst3=3.3.3.3 # VAR ping1=0 ping2=0 ping3=0 # Script start here ping1=$(ping -c 3 -s 8 -o -t 2 $dst1 | awk 'BEGIN{s=""} $2~/bytes/{s=s "X"} END{print s}') ping2=$(ping -c 3 -s 8 -o -t 2 $dst2 | awk 'BEGIN{s=""} $2~/bytes/{s=s "X"} END{print s}') ping3=$(ping -c 3 -s 8 -o -t 2 $dst3 | awk 'BEGIN{s=""} $2~/bytes/{s=s "X"} END{print s}') all=$ping1$ping2$ping3 if [ "$all" == "" ]; then if [ -f /tmp/second.gw ]; then echo "Already switched to secondary" else /sbin/route delete default /sbin/route add default $GW2 /sbin/route add -host $dst1 $GW1 /sbin/route add -host $dst2 $GW1 /sbin/route add -host $dst3 $GW1 /sbin/pfctl -F all -f /etc/pf.conf-rtk touch /tmp/second.gw echo "Primary failed. Switching to secondary" | mail -s "Primary inet channel failed" root fi else if [ -f /tmp/second.gw ]; then /sbin/route delete default /sbin/route add default $GW1 /sbin/route del -host $dst1 $GW1 /sbin/route del -host $dst2 $GW1 /sbin/route del -host $dst3 $GW1 /sbin/pfctl -F all -f /etc/pf.conf echo "Primary restored. Switching back" | mail -s "Primary inet channel restored" root rm /tmp/second.gw fi fi