OpnSense – Reboot wenn WAN nicht erreichbar

Ich betreibe meine OpnSense Firewall hinter einer UPC Giga Connect Box. Diese ist auf Bridge-Modus gestellt, so dass alle Requests direkt an die OpnSense Firewall gelangen.

Hin und wieder scheint UPC etwas zu maintainen. In der Folge verliert OpnSense die connectivity auf dem WAN-Port. Je nachdem wie häufig die UPC Unterhalt betreibt, fliegt die connectivity 1 – 2x pro Monat raus. Einzig ein Reboot löst dann das Problem.

Um dies zu automatisieren, habe ich ein Script hier abgekupfert.

Ich überprüfe die connectivity, indem ich die DNS-Server von Switch und DigitaleGesellschaft anpinge. Die Pings werden nach /root/pingtest.log geschrieben – und NICHT gelöscht – Wenn alles sauber funktioniert, kann das logfile auch jeweils gelöscht werden.
Als erstes ein Config-File ”actions_pingcheck.conf” mit folgenden Inhalt erstellen.

[start]
command:/usr/local/etc/rc.d/ping_check.sh
parameters:
type:script
message:starting ping_check
description:ping_check

Kopiere das File nach

/usr/local/opnsense/service/conf/actions.d

Jetzt das Bash-Script “ping_check.sh” erstellen:

#!/bin/sh

#=====================================================================
# USER SETTINGS
#
# Testing uptime to run script only xx seconds after boot 
#
# Log file
LOGFILE=/root/pingtest.log
#
#=====================================================================


# Current time
curtime=$(date +%s)

# Bootime in seconds
uptime=$(sysctl kern.boottime | awk -F'sec = ' '{print $2}' | awk -F',' '{print $1}')

# Uptime in seconds
uptime=$(($curtime - $uptime))

# If boot is longer than 120 seconds ago...
if [ $uptime -gt 120 ]; then

# A message to the console (I like feedback -if you don't then comment out the echo, wall and rm lines) 
    echo "Testing Connection at" `date +%Y-%m-%d.%H:%M:%S` "uptime:" $uptime "seconds" >> $LOGFILE
    wall $LOGFILE 
    #rm $LOGFILE 

    # Try 1 or 2 minutes worth of very short pings to googles DNS servers - In this case I am only using 10 seconds worth which is the -c 10 value. change this to suit.
    # eg a value of 60 would be pne minute.
    # Quit immediately if we get a single frame back.
    # If neither server responds at all then reboot the firewall.
    counting=$(ping -o -s 0 -c 10 185.95.218.42 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' )

    if [ $counting -eq 0 ]; then

        counting=$(ping -o -s 0 -c 10 130.59.31.248 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' )

        if [ $counting -eq 0 ]; then
            # trying to just restart NIC
            # echo "ping_check: ping fail - trying interface down/up" >> $LOGFILE 
            # wall $LOGFILE 
            # rm $LOGFILE 
            # ifconfig igb0 down
            # ifconfig igb0 up
            sleep 20
            # echo "ping_check: ping fail - Interface reset - trying pings again." >> $LOGFILE 
            #wall $LOGFILE  
            # rm $LOGFILE 
            counting=$(ping -o -s 0 -c 10 185.95.218.42 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' )
            if [ $counting -eq 0 ]; then
                # network down
                echo "Network down - restart Firewall..." >> $LOGFILE
                wall $LOGFILE
                # Save RRD data && clean reboot
                /usr/local/etc/rc.reboot
            fi
        fi
    fi
fi

Kopiere das Bash-Script nach /usr/local/etc/rc.d und stelle sicher, dass es über das execute-flag verfügt

chmod 755 /usr/local/etc/rc.d/ping_check.sh

Nun noch das neu erstellte Script im System als Cron-Job einrichten. Ich führe den Ping alle zwei Minuten durch:

Links