告别复杂配置:3个Shell脚本打造智能路由系统

告别复杂配置:3个Shell脚本打造智能路由系统

【免费下载链接】immortalwrt An opensource OpenWrt variant for mainland China users. 【免费下载链接】immortalwrt 项目地址: https://gitcode.com/GitHub_Trending/im/immortalwrt

你是否还在为OpenWrt路由器的手动配置烦恼? 针对特定地区用户优化的开源路由器系统,提供了丰富的自动化脚本框架。本文将通过3个实用案例,带你掌握Shell脚本开发技巧,实现路由功能智能化。读完本文你将获得:

  • 硬件监控脚本编写方法
  • 网络事件自动响应机制
  • 自定义功能集成最佳实践

环境准备与脚本规范

该系统的脚本系统基于BusyBox Shell,所有自定义脚本需遵循以下规范:

  • 必须以#!/bin/sh开头
  • 权限设置为0755
  • 配置文件统一存放于/etc/config/
  • 可执行文件推荐放置在/usr/bin//sbin/

核心脚本库:

  • 系统函数库:scripts/functions.sh
  • JSON处理工具:/usr/share/libubox/jshn.sh
  • 事件触发机制:/etc/hotplug.d/

案例1:硬件温度监控与风扇控制

温度采集原理

路由器核心组件温度可通过sysfs接口读取,不同硬件路径可能存在差异:

# CPU温度 (MVEBU平台)
CPU_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon2/temp1_input)
# 内存温度
DDR_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon1/temp1_input)
# 无线模块温度
WIFI_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon1/temp2_input)

完整控制脚本

target/linux/mvebu/cortexa9/base-files/sbin/fan_ctrl.sh实现了基于温度阈值的智能调速:

#!/bin/sh

CPU_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon2/temp1_input)
DDR_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon1/temp1_input)
WIFI_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon1/temp2_input)

# 温度阈值定义
CPU_LOW=85
CPU_HIGH=95
DDR_LOW=65
DDR_HIGH=75
WIFI_LOW=100
WIFI_HIGH=115

# 自动检测风扇控制路径
if [ -d /sys/devices/pwm_fan ];then
    FAN_CTRL=/sys/devices/pwm_fan/hwmon/hwmon0/pwm1
elif [ -d /sys/devices/platform/pwm_fan ];then
    FAN_CTRL=/sys/devices/platform/pwm_fan/hwmon/hwmon0/pwm1
else
    exit 0
fi

# 三段式调速逻辑
if [ "$CPU_TEMP" -ge "$CPU_HIGH" -o "$DDR_TEMP" -ge "$DDR_HIGH" -o "$WIFI_TEMP" -ge "$WIFI_HIGH" ];then
    echo "255" > $FAN_CTRL  # 全速
elif [ "$CPU_TEMP" -ge "$CPU_LOW" -o "$DDR_TEMP" -ge "$DDR_LOW" -o "$WIFI_TEMP" -ge "$WIFI_LOW" ];then
    echo "100" > $FAN_CTRL  # 中速
else
    echo "0" > $FAN_CTRL    # 停止
fi

部署与测试

  1. 复制脚本到目标路径:
scp fan_ctrl.sh root@192.168.1.1:/sbin/
  1. 添加执行权限:
chmod 0755 /sbin/fan_ctrl.sh
  1. 设置定时任务(/etc/crontabs/root):
*/5 * * * * /sbin/fan_ctrl.sh  # 每5分钟执行一次

案例2:DHCP事件自动响应

当设备接入网络时,该系统通过dnsmasq触发DHCP事件,可在package/network/services/dnsmasq/files/dhcp-script.sh中添加自定义逻辑:

事件类型与参数

支持的事件类型:

  • add:新设备获取IP
  • del:设备断开连接
  • arp-add:检测到新ARP记录
  • tftp:TFTP传输事件

核心代码片段:

case "$1" in
    add)
        json_add_string "" "ACTION=add"
        json_add_string "" "HOSTNAME=$4"
        hotplugobj="dhcp"
    ;;
    del)
        json_add_string "" "ACTION=remove"
        json_add_string "" "HOSTNAME=$4"
        hotplugobj="dhcp"
    ;;
    # 其他事件类型...
esac

扩展应用:设备接入通知

add事件处理中添加:

# 发送通知到手机APP
curl -X POST http://push.example.com/api \
  -d "device=$4" \
  -d "ip=$3" \
  -d "time=$(date +%Y%m%d%H%M%S)"

案例3:多平台风扇控制适配

不同硬件平台的温度传感器路径存在差异,如Realtek平台的电源温度监控脚本target/linux/realtek/base-files/sbin/fan_ctrl.sh

#!/bin/sh

PSU_TEMP=$(cut -c1-2 /sys/class/hwmon/hwmon0/temp1_input)
FAN_CTRL='/sys/class/hwmon/hwmon0/pwm1'
PSU_THRESH=51000  # 51°C

if [ "$PSU_TEMP" -ge "$PSU_THRESH" ];then
    echo "250" > $FAN_CTRL  # 高温档
else
    echo "156" > $FAN_CTRL  # 常温档
fi

平台适配技巧

  1. 使用uname -m判断架构:
case $(uname -m) in
    armv7*)
        # MVEBU平台路径
        TEMPPATH=/sys/class/hwmon/hwmon2
    ;;
    aarch64*)
        # Realtek平台路径
        TEMPPATH=/sys/class/hwmon/hwmon0
    ;;
esac
  1. 创建设备配置文件/etc/config/hwmon
config sensor 'cpu'
    option path '/sys/class/hwmon/hwmon2'
    option low '85'
    option high '95'

高级应用:脚本模块化与配置管理

配置文件设计规范

推荐使用UCI配置系统,例如创建/etc/config/monitor

config monitor 'temperature'
    option interval '30'  # 检测间隔(秒)
    option log_level '1'   # 日志级别

config threshold 'cpu'
    option low '80'
    option high '90'
    option action 'fan'    # 关联动作

模块化脚本框架

#!/bin/sh

. /lib/functions.sh
. /usr/share/libubox/jshn.sh

# 加载配置
config_load 'monitor'
config_get interval temperature interval 30

# 定义动作函数
action_fan() {
    local level=$1
    # 调用风扇控制逻辑
}

# 主循环
while true; do
    check_temperatures  # 温度检测
    handle_alerts       # 告警处理
    sleep $interval
done

调试与部署最佳实践

脚本调试技巧

  1. 添加详细日志:
LOG_FILE="/tmp/script.log"
log() {
    echo "[$(date +%H:%M:%S)] $1" >> $LOG_FILE
}

log "Temperature check started"
  1. 使用set -x开启调试模式:
#!/bin/sh -x  # 开头添加-x参数

开机自启动配置

创建/etc/init.d/monitor

#!/bin/sh /etc/rc.common

START=95  # 启动顺序
STOP=10   # 停止顺序

start() {
    /usr/bin/temp_monitor.sh &
}

stop() {
    killall temp_monitor.sh
}

启用自启动:

/etc/init.d/monitor enable
/etc/init.d/monitor start

总结与扩展方向

本文介绍的3个案例覆盖了硬件监控、事件响应和平台适配等核心场景,基于这些基础可以扩展更多功能:

  • 带宽使用统计与限制
  • 设备接入控制(MAC过滤)
  • 特定连接自动切换
  • 定时任务与网络唤醒

该系统提供了完整的脚本开发生态,更多示例可参考:

通过掌握Shell脚本开发,你可以将普通路由器改造成智能网络中枢,实现个性化需求。建议从简单功能入手,逐步构建复杂系统,同时遵循项目规范以便后续升级维护。

【免费下载链接】immortalwrt An opensource OpenWrt variant for mainland China users. 【免费下载链接】immortalwrt 项目地址: https://gitcode.com/GitHub_Trending/im/immortalwrt

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值