如何在批处理文件中等待响应并恢复工作?

如何在批处理文件中等待响应并恢复工作?

我的网络连接大约每小时断开一次 10-60 秒。

为了进行诊断,我找到并改编了一个批处理脚本(ping日志) 每隔 x 秒 ping google.com 一次,并将输出保存到文本文件。这会告诉我网络中断发生的时间。

如何扩展 ping_log.bat 以便它调用网络诊断网络超时(ping 命令超时)时,文件是否会恢复正常运行?超时结束后,文件是否会恢复正常运行?

ping_log.bat(基于 SO 上的一篇文章),长句摘自StackOverflow:在 Windows CLI 上使用时间戳进行 Ping

@echo off
call network_diagnostics.bat REM this line needs to be called only when the ping command times out

ping -t www.google.com|cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 www.google.com>nul" >> log.txt

输出文件的样子:

13:43:34.96 Pinging www.google.com [142.250.74.35] with 32 bytes of data:
13:43:20.00 Reply from 142.250.74.35: bytes=32 time=13ms TTL=52
13:43:25.07 Reply from 142.250.74.35: bytes=32 time=13ms TTL=52
13:43:30.13 Reply from 142.250.74.35: bytes=32 time=13ms TTL=52
13:43:35.20 Reply from 142.250.74.35: bytes=32 time=14ms TTL=52
13:43:45.24 Request timed out.
13:43:50.31 Reply from 142.250.74.35: bytes=32 time=14ms TTL=52

网络诊断

ipconfig /all

REM This file actually contains more, but in order to produce a Minimum Viable Code Example, I am showing only a single line here.

相关内容