Help with Scripting (change default gateway)

Started by Amr, May 30, 2020, 09:13:52 AM

Previous topic - Next topic
Hello guys,
I'm trying to write a script to switch between gateways (the machine has only 1 wan interface) when one goes down by pinging a remote host, triggered by a cron-job.
my problem is I can't change the default gateway when I type
route delete default
route add default 192.168.1.1
and checking the gateway by typing
netstat -nr
It shows that the changes were made but going to the GUI nothing changed and by reloading the interface my changes are overwritten.

Also, I'd like to know the variable that holds the current gateway value.
Disclaimer: All advice presented is "AS IS", no warranties.
I'm not part of the opnsense team, just trying to help.

Why don't you just add a second gateway with a lower priority? As soon as first fails, the second is used. No need for scripting.

https://docs.opnsense.org/manual/gateways.html
Intel(R) Xeon(R) Silver 4116 CPU @ 2.10GHz (24 cores)
256 GB RAM, 300GB RAID1, 3x4 10G Chelsio T540-CO-SR

hello hbc,

QuoteWhy don't you just add a second gateway with a lower priority?
Well I tried it once, but it didn't work so I assumed that the gateway group functionality is limited to multiple wan interfaces setup, and since I have only one wan interface (right now) I thought of scripting, If you can confirm that gateway groups work in single wan interface might give it a try.

thanks for your help.
Disclaimer: All advice presented is "AS IS", no warranties.
I'm not part of the opnsense team, just trying to help.

I did not mention gateway groups. Just multiple gateways with different priorities. But gateway group should work, too.
Intel(R) Xeon(R) Silver 4116 CPU @ 2.10GHz (24 cores)
256 GB RAM, 300GB RAID1, 3x4 10G Chelsio T540-CO-SR