小米路由添加开机启动

为小米路由添加自定义开机启动

前置:使用 https://github.com/openwrt-xiaomi/xmir-patcher 解锁 ssh。

添加开机启动脚本

脚本需要使用 chmod +x /etc/crontabs/patches/*.sh 添加执行权限

  1. 使用 uci 添加 firewall 配置:
1
2
3
4
5
6
uci set firewall.startup=include
uci set firewall.startup.type='script'
uci set firewall.startup.path='/etc/crontabs/patches/startup.sh'
uci set firewall.startup.enabled='1'

uci commit firewall
  1. 自定义脚本内容:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/sh
# /etc/crontabs/patches/startup.sh

[ -e "/tmp/startup.log" ] && return 0

# 执行命令
echo "Hello World!"

# 异步执行 startup_delay.sh
( sleep 20; /etc/crontabs/patches/startup_delay.sh ) &

echo "Done!" > /tmp/startup.log
  1. 延迟执行脚本

因为启动顺序问题,部分命令可能需要延迟执行,所以增加了 startup_delay.sh。

这里三条命令是解决光猫使用桥接模式+小米路由播号,无法在内网访问光猫的问题。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/bin/sh
# /etc/crontabs/patches/startup_delay.sh

[ -e "/tmp/startup_delay.log" ] && return 0

# 给 eth0 添加光猫网段地址 192.168.1.2
ifconfig eth0:1 192.168.1.2 netmask 255.255.255.0 up

# 使用 iptables 转发内网流量
iptables -I FORWARD -i br-lan -d 192.168.1.0/24 -j ACCEPT
iptables -t nat -I POSTROUTING -o eth0 -d 192.168.1.0/24 -j MASQUERADE

echo "Done!" > /tmp/startup_delay.log
使用 Hugo 构建
主题 StackJimmy 设计