前提是已经固化了 SSH,这部分请自行解决,之后也会看情况出教程。

我没有找到方法开机自动执行脚本,参考的一些教程都没有作用:

https://www.right.com.cn/forum/thread-8223975-1-1.html

https://post.smzdm.com/p/a5o6wzkx/

https://www.right.com.cn/forum/thread-8340357-1-1.html

因此我选择了一种折衷的方式,就是使用另一台机器定时自动执行脚本,然后关闭路由器的防火墙。

首先在路由器 /data 下面新建一个脚本:

bash
  • 01
  • 02
  • 03
  • 04
  • 05
#!/bin/sh ip6tables -F ip6tables -X ip6tables -P INPUT ACCEPT ip6tables -P OUTPUT ACCEPT ip6tables -P FORWARD ACCEPT

保存后,添加执行权限:

    chmod +x startup_script.sh

    在另一台主机中,添加一个脚本,保存为 closefire.sh:

    • 01
    • 02
    • 03
    • 04
    • 05
    • 06
    • 07
    • 08
    • 09
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    GNU nano 7.2 closefire.sh #!/bin/sh # 这里需要替换成自己的密码 REMOTE_HOST="192.168.x.x" REMOTE_USER="root" REMOTE_PASSWORD="xxxx" REMOTE_SCRIPT_PATH="/data/startup_script.sh" # 使用 sshpass 来提供密码,实现非交互式登录 注意:在脚本中硬编码密码存在安全风险,建议使用 SSH 密钥对进行身份验证。 if ! command -v sshpass >/dev/null 2>&1; then echo "错误:sshpass 命令未找到。请先安装 sshpass。" echo "例如:在 Debian/Ubuntu 上执行 'sudo apt-get install sshpass'" echo " 在 CentOS/RHEL 上执行 'sudo yum install sshpass'" exit 1 fi # 执行远程命令 sshpass -p "$REMOTE_PASSWORD" ssh -o StrictHostKeyChecking=no $REMOTE_USER@$REMOTE_HOST "$REMOTE_SCRIPT_PATH" # 检查命令执行结果 if [ $? -eq 0 ]; then echo "成功在 $REMOTE_HOST 上执行了 $REMOTE_SCRIPT_PATH。" else echo "在 $REMOTE_HOST 上执行 $REMOTE_SCRIPT_PATH 失败,返回码:$?" fi exit $?

    添加可执行权限:

      chmod +x closefire.sh

      当你手动执行:

        sh closefire.sh

        通常会报错:

        这就需要修改 ssh 的设置,使用文本编辑器打开本地机器 /etc/ssh/ssh_config 文件(需要管理员权限):

          sudo nano /etc/ssh/ssh_config
          • 找到包含 PermitRootLogin 的那一行。

          • 删除该行或者将其注释掉(在行首添加 # 符号):

          然后又会报错:

            Unable to negotiate with 192.168.3.1 port 22: no matching host key type found. Their offer: ssh-rsa
            • 出于安全考虑,较新版本的 OpenSSH 客户端可能会默认禁用或降低对较旧且被认为不太安全的 ssh-rsa 算法的支持(特别是当它使用 SHA-1 签名时)。

            • 解决方法: 你可以临时允许 ssh-rsa 算法在你的本地 SSH 客户端连接时使用。编辑你的 SSH 客户端配置文件 /etc/ssh/ssh_config(或者你的用户级别配置文件 ~/.ssh/config)。

            • 01
            mkdir ~/.ssh nano ~/.ssh/config
            • 在文件的末尾添加或修改 Host 部分,针对特定的主机或所有主机添加以下行:

            • 01
            Host 192.168.x.x # 这是小米路由器的ip HostKeyAlgorithms +ssh-rsa

            此时执行以下命令:

              sshpass -p "root" ssh -o StrictHostKeyChecking=no -o HostKeyAlgorithms=+ssh-rsa root@192.168.x.x "/data/startup_script.sh"

              最后,定时执行本地机器中的 closefire.sh 脚本:

                (crontab -l 2>/dev/null; echo "0 0 * * * xxx/closefire.sh") | crontab - # 需要替换为你的脚本路径

                检查:

                  crontab -l

                  输出:

                  • 01
                  • 02
                  • 03
                  • 04
                  • 05
                  • 06
                  • 07
                  • 08
                  • 09
                  • 10
                  • 11
                  • 12
                  • 13
                  • 14
                  • 15
                  • 16
                  • 17
                  • 18
                  • 19
                  • 20
                  # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any'). # # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command 0 0 * * * /yourpath/closefire.sh # 每天凌晨执行一次