feat: automatically match WAN interface name in OpenWRT environment#20
feat: automatically match WAN interface name in OpenWRT environment#20Tamako0401 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request adds automatic WAN interface detection for OpenWrt and ImmortalWrt routers to address naming inconsistencies across different versions. The change eliminates the need for OpenWrt users to manually remember and specify the WAN interface name (which varies between versions as wan, eth0.x, etc.) by querying the uci configuration system.
Changes:
- Added
get_default_interface()function to automatically detect WAN interface on OpenWrt/ImmortalWrt systems - Updated OpenWrt documentation to reflect that the
-iparameter is now optional - Simplified crontab example in OpenWrt section by removing the
-iparameter
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| NJUPT-AutoLogin.sh | Implements automatic WAN interface detection via new get_default_interface() function that queries uci on OpenWrt/ImmortalWrt systems |
| README.md | Updates OpenWrt section documentation to indicate auto-detection support and removes manual interface specification instructions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| get_default_interface() { | ||
| local def_if="eth0" | ||
| if [[ -f "/etc/os-release" ]]; then | ||
| # shellcheck source=/dev/null | ||
| . /etc/os-release | ||
| if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then | ||
| if command -v uci >/dev/null 2>&1; then | ||
| local wan_if | ||
| wan_if=$(uci -q get network.wan.device) | ||
| if [[ -z "$wan_if" ]]; then | ||
| wan_if=$(uci -q get network.wan.ifname) | ||
| fi | ||
| if [[ -n "$wan_if" ]]; then | ||
| def_if="$wan_if" | ||
| fi | ||
| fi | ||
| fi | ||
| fi | ||
| printf "%s" "$def_if" | ||
| } |
There was a problem hiding this comment.
The new default interface logic breaks macOS compatibility. The function defaults to "eth0" for all non-OpenWrt systems, but macOS uses "en0" as the default Wi-Fi interface (as shown in the usage example on line 20). This will cause the script to fail on macOS unless users explicitly specify the -i parameter.
The function should check the system environment and return "en0" for macOS (Darwin) and "eth0" for Linux systems, then apply the OpenWrt-specific detection on top of that. For example:
get_default_interface() {
local def_if="eth0"
# Set platform-specific defaults
if [[ "$sysenv" == "Darwin" ]]; then
def_if="en0"
fi
# Override with OpenWrt/ImmortalWrt detection if applicable
if [[ -f "/etc/os-release" ]]; then
. /etc/os-release
if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then
if command -v uci >/dev/null 2>&1; then
local wan_if
wan_if=$(uci -q get network.wan.device)
if [[ -z "$wan_if" ]]; then
wan_if=$(uci -q get network.wan.ifname)
fi
if [[ -n "$wan_if" ]]; then
def_if="$wan_if"
fi
fi
fi
fi
printf "%s" "$def_if"
}| if [[ -f "/etc/os-release" ]]; then | ||
| # shellcheck source=/dev/null | ||
| . /etc/os-release | ||
| if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then | ||
| if command -v uci >/dev/null 2>&1; then | ||
| local wan_if | ||
| wan_if=$(uci -q get network.wan.device) | ||
| if [[ -z "$wan_if" ]]; then | ||
| wan_if=$(uci -q get network.wan.ifname) | ||
| fi | ||
| if [[ -n "$wan_if" ]]; then | ||
| def_if="$wan_if" | ||
| fi | ||
| fi | ||
| fi | ||
| fi |
There was a problem hiding this comment.
The OpenWrt/ImmortalWrt detection logic in this function duplicates the existing check_openwrt() function (lines 499-508). Consider refactoring to use the existing function to avoid code duplication. This would improve maintainability and ensure consistency across the codebase.
For example, the function could be restructured as:
get_default_interface() {
local def_if="eth0"
if [[ "$sysenv" == "Darwin" ]]; then
def_if="en0"
fi
if check_openwrt; then
if command -v uci >/dev/null 2>&1; then
local wan_if
wan_if=$(uci -q get network.wan.device)
if [[ -z "$wan_if" ]]; then
wan_if=$(uci -q get network.wan.ifname)
fi
if [[ -n "$wan_if" ]]; then
def_if="$wan_if"
fi
fi
fi
printf "%s" "$def_if"
}However, note that this would require defining check_openwrt() before get_default_interface(), or moving the function definition order in the file.
| if [[ -f "/etc/os-release" ]]; then | |
| # shellcheck source=/dev/null | |
| . /etc/os-release | |
| if [[ "$NAME" == "OpenWrt" ]] || [[ "$NAME" == "ImmortalWrt" ]]; then | |
| if command -v uci >/dev/null 2>&1; then | |
| local wan_if | |
| wan_if=$(uci -q get network.wan.device) | |
| if [[ -z "$wan_if" ]]; then | |
| wan_if=$(uci -q get network.wan.ifname) | |
| fi | |
| if [[ -n "$wan_if" ]]; then | |
| def_if="$wan_if" | |
| fi | |
| fi | |
| fi | |
| fi | |
| if [[ "$sysenv" == "Darwin" ]]; then | |
| def_if="en0" | |
| fi | |
| if check_openwrt; then | |
| if command -v uci >/dev/null 2>&1; then | |
| local wan_if | |
| wan_if=$(uci -q get network.wan.device) | |
| if [[ -z "$wan_if" ]]; then | |
| wan_if=$(uci -q get network.wan.ifname) | |
| fi | |
| if [[ -n "$wan_if" ]]; then | |
| def_if="$wan_if" | |
| fi | |
| fi | |
| fi |
因为 OpenWrt 新旧版本的网络栈命名差异,导致 WAN 接口名称不同(不同于文档中的

eth0.x,可能会显示名称为wan),容易引起误导。此更改实现了 OpenWrt 或 ImmortalWrt 环境的 WAN 接口名称的自动匹配,无需在路由器后台记住任何东西。此更改在仙林校区测试可用。