OPNsense Forum

Archive => 20.1 Legacy Series => Topic started by: ooker on July 29, 2020, 04:24:00 pm

Title: SOLVED: Auto-Re-connect WAN after cable modem re-connects
Post by: ooker 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.

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

Title: Re: Help Wanted: Auto-Re-connect WAN after cable modem re-connects
Post by: krawc on July 29, 2020, 06:07:14 pm
I just started to use opnsense, run all updates and also get similar issue.  Re-boot helped initially but second time around then ISP renewed the IP lease, I have lost ability to connect to https port 443.  Not sure if this is related.  I was still able to ping DNS or ip entries on the internet.

I had switched back to my TPLink router and noticed that my new IP lease is completely different to what the Opnsense had.  So either i got new IP or Opnsense did not update properly.
Title: Re: SOLVED: Auto-Re-connect WAN after cable modem re-connects
Post by: littlepepper on July 30, 2020, 04:28:34 am
Usually DHCP on ISP side is based on MAC addresses, so if you switch from TPLink router to your OpnSense box, unless you are spoofing the MAC address, the IP will be different.

I just started to use opnsense, run all updates and also get similar issue.  Re-boot helped initially but second time around then ISP renewed the IP lease, I have lost ability to connect to https port 443.  Not sure if this is related.  I was still able to ping DNS or ip entries on the internet.

I had switched back to my TPLink router and noticed that my new IP lease is completely different to what the Opnsense had.  So either i got new IP or Opnsense did not update properly.