On Wed, 8 Jun 2022, BERTRAND Joël wrote:

        Your script runs, but SEC is only a backup and I have to switch back to
PRI as soon as possible (PRI has a fixed IP address).


That's possible too. Just fill in the is_up_primary()--telnet: nc -t,
https: curl -S, snmp, whatever--to query the status of the primary
router's outbound connection and return true or false:

```
#!/bin/sh

PRI=192.168.15.19
SEC=192.168.15.20
R=$PRI
H=8.8.8.8
T=10

is_up_primary() {
        # if curl -S http://$PRI/... | grep -q ...
        # then  true
        # else  false
        # fi
        return $((RANDOM % 2))
}
sw_route() {
        local R=$1
        route add default gw $R
        echo switched to $R
}
while :
do      if ! ping -nc1 $H >/dev/null 2>&1
        then    case $R in
                $PRI)   R=$SEC ;;
                $SEC)   R=$PRI ;;
                esac
                sw_route $R
                S=1
        else    case $R in
                $SEC)   if is_up_primary
                        then    R=$PRI
                                sw_route $R
                        fi
                        ;;
                esac
                S=$T
        fi
        sleep $S
done
```

-RVP

Reply via email to