1
20.1 Legacy Series / SOLVED: Auto-Re-connect WAN after cable modem re-connects
« on: July 29, 2020, 04:24:00 pm »
Summary: When the WAN connection goes down, it does not re-request DHCP for the WAN address, so OPNsense has no WAN gateway and can't communicate with the Internet until the OPNsense box is restarted.
Does anyone know of a setting or script that could detect los of WAN address (a good symptom seems to be no WAN gateway) and keep trying to re-request WAN DHCP until a result is received?
I have Comcast and sometimes the cable modem goes offline and comes back on. When this happens my OPNsense box loses connectivity to the WAN. The cable modem status shows that it has connectivity, so all that is needed is for the OPNsense box to re-request DHCP for the WAN connection. I don't know of a way to automatically trigger this. I also don't know the best way to trigger a WAN re-request of DHCP via a script. Has anyone solved this or does anyone have info on the best way to trigger a WAN re-request via a script/cron job?
UPDATE: I've added this script as a cron job on my OPNsense box that runs every minute, and it seems to work (probably needs more testing, but at least it worked for the common case that impacts me). I hope this helps someone else, and if you see a better way to do this, please reply with your suggestions.
Caveat: I only have one WAN link and it is the default route for my OPNsense box.
Note: I have entered my WAN interface as a string in the script (wanInterface="em0").
Question: Does anyone know of a good way to get the WAN interface by looking at config files and/or via some shell command? I'd rather not have hard-coded interface names in my script.
Does anyone know of a setting or script that could detect los of WAN address (a good symptom seems to be no WAN gateway) and keep trying to re-request WAN DHCP until a result is received?
I have Comcast and sometimes the cable modem goes offline and comes back on. When this happens my OPNsense box loses connectivity to the WAN. The cable modem status shows that it has connectivity, so all that is needed is for the OPNsense box to re-request DHCP for the WAN connection. I don't know of a way to automatically trigger this. I also don't know the best way to trigger a WAN re-request of DHCP via a script. Has anyone solved this or does anyone have info on the best way to trigger a WAN re-request via a script/cron job?
UPDATE: I've added this script as a cron job on my OPNsense box that runs every minute, and it seems to work (probably needs more testing, but at least it worked for the common case that impacts me). I hope this helps someone else, and if you see a better way to do this, please reply with your suggestions.
Caveat: I only have one WAN link and it is the default route for my OPNsense box.
Note: I have entered my WAN interface as a string in the script (wanInterface="em0").
Question: Does anyone know of a good way to get the WAN interface by looking at config files and/or via some shell command? I'd rather not have hard-coded interface names in my script.
Code: [Select]
#!/bin/sh
gatewayIP=$(netstat -4rn | grep default | awk '{print $2}')
wanInterface="em0"
echo "Gateway: $gatewayIP"
echo "WAN Interface: $wanInterface"
if [ -z $gatewayIP ]
then
echo "NO Gateway"
#Bring the interface down then up to renew the WAN DHCP
ifconfig $wanInterface down
ifconfig $wanInterface up
else
# if return = 0 then host is reachable
ping -c 1 $gatewayIP > /dev/null
if [ $? -eq 0 ]
then
echo "Gateway Reachable"
else
echo "Gateway Unreachable"
#Bring the interface down then up to renew the WAN DHCP
ifconfig $wanInterface down
ifconfig $wanInterface up
fi
fi