Skip to content

Instantly share code, notes, and snippets.

@indraAsLesmana
Last active September 10, 2025 09:12
Show Gist options
  • Save indraAsLesmana/cad8d0689d7be73743e5d32123e607f2 to your computer and use it in GitHub Desktop.
Save indraAsLesmana/cad8d0689d7be73743e5d32123e607f2 to your computer and use it in GitHub Desktop.
Orbit Pro HKM281: Auto Restart WWAN Interface OpenWRT
#!/bin/sh
# =============================================================================
#
# OpenWrt LTE/Cellular Connection Watchdog
# Created by: Indra L.N.
#
# =============================================================================
#
# PURPOSE:
# This script monitors an OpenWrt cellular connection that uses a virtual
# interface (common with NCM, QMI, and MBIM protocols) to get an IP address.
# It is designed to solve a common problem where the modem appears connected,
# but the virtual IP interface disappears, cutting off internet access.
#
# When a failure is detected, this script automatically restarts the main
# modem interface to re-establish a working connection.
#
# =============================================================================
#
# HOW TO USE:
#
# 1. CUSTOMIZE VARIABLES (If Necessary)
# - Edit the VIRTUAL_IF and MODEM_IF variables below to match your
# interface names as they appear in LuCI or from the `ifconfig` command.
#
# 2. UPLOAD SCRIPT TO YOUR ROUTER
# - Copy this file to the /usr/bin/ directory on your OpenWrt router.
# - Example using scp from your computer:
# scp ./check_wwan.sh [email protected]:/usr/bin/
#
# 3. MAKE THE SCRIPT EXECUTABLE
# - SSH into your router and run the following command:
# chmod +x /usr/bin/check_wwan.sh
#
# 4. SCHEDULE THE SCRIPT WITH CRON
# - On your router, edit the root user's crontab file:
# vi /etc/crontabs/root
# - Add the following line to the file. This will run the script
# every 5 minutes. Ensure the file ends with a blank newline.
#
# */5 * * * * /usr/bin/check_wwan.sh
#
# 5. ENABLE AND START THE CRON SERVICE
# - Run these commands to make sure the scheduler is active and
# will start automatically on reboot:
# /etc/init.d/cron enable
# /etc/init.d/cron start
#
# =============================================================================
#
# VERIFICATION:
# The script is silent and will only produce output when it detects a problem.
# To see if it's working, you can monitor the system log:
#
# logread -f
#
# When your connection drops, a message like this will appear in the log:
# "Watchdog: Interface wwan_4 not found. Restarting wwan..."
#
# =============================================================================
# --- Script Configuration ---
# The virtual interface that gets the IP address (the one to check)
VIRTUAL_IF="wwan_4"
# The main modem interface (the one to restart)
MODEM_IF="wwan"
# --- Script Logic ---
# Check if the virtual interface exists and is up.
ifstatus $VIRTUAL_IF >/dev/null 2>&1
# Check the exit code of the last command ($?). A non-zero code means
# ifstatus failed, which tells us the interface is down or doesn't exist.
if [ $? -ne 0 ]; then
# The virtual interface is down.
# Now, let's validate the main modem interface before we restart it.
uci show network.$MODEM_IF >/dev/null 2>&1
if [ $? -eq 0 ]; then
# This condition is true if the wwan interface exists in the configuration.
logger "Watchdog: Interface $VIRTUAL_IF not found. Restarting $MODEM_IF..."
ifdown $MODEM_IF && ifup $MODEM_IF
else
# This condition is true if the wwan interface does NOT exist in the configuration.
logger "Watchdog: Interface $VIRTUAL_IF not found, but main interface $MODEM_IF is not configured. Taking no action."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment