当我的网络中断时如何重新启动计算机?

当我的网络中断时如何重新启动计算机?

我正在运行一台 Windows 10 计算机,我需要能够在外出时远程访问它。我在这台计算机上使用 VPN 服务,当长时间处于开启状态时,连接经常会断开,使系统处于 VPN 客户端认为它仍处于连接状态但实际上并未连接的状态。客户端从不尝试重新连接,因为它认为它仍处于连接状态,并且非 VPN 流量也被阻止(就像一种无意的终止开关)。这导致无意的终止开关阻止我远程访问我的计算机。

当互联网无法连接超过 5 分钟时自动重启计算机是我的最终目标,但我不知道该怎么做。如果有人能帮我设计一个脚本,在这种情况下重启计算机,我将不胜感激。我对 cmd 或 powershell 脚本不太熟悉,但这里有一个小伪代码演示了我想要编写脚本的过程,以便可以使用我的操作系统附带的内置工具/软件(例如 powershell 或 batch)来运行它。

// Note- PSEUDO-CODE, NOT A USABLE SCRIPT AS-IS
ping 8.8.8.8 // check for internet connection. 0% packet returns would indicate the computer had probably entered this network lock-up
if pingsReturned == 0
{
    wait 5 minutes // in case of temporary disconnect, give the network time to reestablish itself
    ping superuser.com // alternate site used to confirm google isn't the only site I can't connect to, i.e. google is down, not my network connectivity
    if pingsReturned == 0
        shutdown /f /r /t 60 /c "Rebooting due to connection issues." 
    else
    {
        exit
    }
}
else
{
    exit
}

如果可能的话,我计划将其作为任务计划程序中每隔 5 分钟运行一次的任务来运行。我搜索了互联网(特别是这个网站)寻找解决方案,但没有找到适用于 Windows 的解决方案,不过我确实找到了一个非常类似的 Linux 设置实现(巧合的是,它也使用了 Google 的 DNS 作为测试,或者也许这只是一个用于一般 ping 测试的网站……)。

答案1

我想这样的事情应该可以解决问题

:: ping google
ping www.google.com -n 1 | find "TTL=" >nul
if errorlevel 1 (
    :: wait 60 seconds
    ping 127.0.0.1 -n 60 > nul

    :: ping another
    ping www.facebook.com -n 1 | find "TTL=" >nul
    if errorlevel 1 (
        echo Rebooting...
        shutdown /r
    ) else (
        echo We're back up again
    )
) else (
    echo We're up
)

相关内容