【问题标题】:Restarting router if offline离线重启路由器
【发布时间】:2018-06-17 11:38:41
【问题描述】:

我奇怪的互联网连接每天都掉线,很奇怪,但我只需要重新启动路由器就可以了。

所以我决定编写脚本来重启路由器,一旦它下线。

ping -c 是我想到的第一个想法,

经过思考和研究,我想出了这个:

wget -q --spider http://google.com
if [ $? -eq 0 ]; then
    echo "Online"
    # do nothing
else
    echo "Offline"
    # retry for the next 2 minutes
    # and when still offline
    # curl command to restart router
fi

用那 2 分钟,它可以补偿路由器重启所需的时间,问题是我如何在接下来的 2 分钟内重试。

将其保存在文件中,并作为 cron 每 7 分钟运行一次,我认为可以为我完成这项工作。

【问题讨论】:

    标签: bash cron wget


    【解决方案1】:

    我可能有答案

    #!/bin/bash
    
    retries=0
    
    detect_network () {
      wget -q --spider http://google.com
      if [ $? -eq 0 ]; then
        echo "Online"
        retries=0
      else
        if [ "$retries" -eq 120 ]; then
          echo "curl now"
          #
        else
          echo "Offline"
          ((retries=retries+1))
          sleep 1
          detect_network
        fi
      fi
    }
    
    detect_network
    

    【讨论】:

      猜你喜欢
      • 2020-02-25
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2021-07-18
      • 2018-04-13
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多