Vbscript 如果连续ping失败,启动跟踪路由的脚本,输出到日志

Vbscript 如果连续ping失败,启动跟踪路由的脚本,输出到日志,vbscript,batch-file,ping,traceroute,Vbscript,Batch File,Ping,Traceroute,我想持续ping我的家庭公共IP地址,如果ping失败,则自动执行跟踪路由以查看失败的位置 我一直在尝试跟随这里的评论: 最好与Windows兼容,最好是bat或vbs 从互联网上的任何地方,我都会失去与家庭网络的连接。在工作中,我启动了一个ping,当它掉下来时,我做了一个traceroute,它在到达我的IP之前就失败了 我需要一个日志文件来证明它不是我的调制解调器、路由器或计算机。您可以制作一个简单的批处理文件,尝试ping,如果失败,则执行tracert,例如: setlocal se

我想持续ping我的家庭公共IP地址,如果ping失败,则自动执行跟踪路由以查看失败的位置

我一直在尝试跟随这里的评论:

最好与Windows兼容,最好是bat或vbs

从互联网上的任何地方,我都会失去与家庭网络的连接。在工作中,我启动了一个ping,当它掉下来时,我做了一个traceroute,它在到达我的IP之前就失败了


我需要一个日志文件来证明它不是我的调制解调器、路由器或计算机。

您可以制作一个简单的批处理文件,尝试ping,如果失败,则执行tracert,例如:

setlocal
set host=www.bigpond.com
set logfile=nettest.log
echo %date% %time%>>%logfile%
ping %host%>>%logfile%
if ERRORLEVEL 1 tracert %host%>>%logfile
endlocal
这里有很大的改进空间

然后创建一个计划任务,每五分钟运行一次,或者任何适合您的任务

或者,您可以包含一个带有“sleep”的循环。有一个穷人的睡眠使用:

choice /d y /t 5 > nul
基本上,如果它发现一行包含单词
Request
(仅在无法ping地址时出现)的实例,则该状态执行
ping
,执行tracert。
PING
中的
-n
-w
开关告诉它只跳转一次,在没有收到响应1秒后超时。如果您正在ping本地主机,这是非常好的。
语句的第二个
是有一个停止点。将
1400
更改为您希望脚本停止的时间(当然是在军事时间)

如果有人需要的话,这就是我最后要做的。本质上,“Ping-n127.0.0.1>Nul”是添加一个5秒的计数器,这样它每5秒只Ping一次目标,5可以更改为所需的任何值

Windows 7存在此问题,ping可能会导致类似“来自192.168.1.5的回复:无法访问目标主机”的结果。因此,它不会出错,而是从自身得到一个回复,而不是错误级别1。 与查找错误级别1不同,我选择查找具有“%SystemRoot%\system32\ping.exe-n 1%Address%\124;%SystemRoot%\system32\find.exe”TTL=“>NUL”的TTL的无结果

不管怎样,我相信这里的其他答案非常相似,可能已经奏效了,所以我对它们进行了排名,但将其标记为答案


谢谢大家

我一直在寻找同样的东西来调查为什么VPN在有线连接上不断下降,使用了上面的批处理文件建议之一,这很好。
@echo off
set Address=www.google.com
set LogDir=C:\pingtest
md %LogDir%
%SystemRoot%\explorer.exe "%LogDir%"
echo PingTest script to monitor network connection.  Control-C to exit.
echo Tests connection by pinging %Address%.  Logs to %LogDir%\logfile.log.
echo %date% %time% Initial tracert (trace route) to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
:Loop
REM  5 second delay
PING -n 5 -w 1 127.0.0.1>nul
echo %date% %time% Pinging %Address%
echo %date% %time% Pinging %Address% >> %LogDir%\logfile.log
%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL
if %ERRORLEVEL% EQU 0 goto :Loop
echo %date% %time% PING ERROR - Tracing route to %Address%
echo %date% %time% PING ERROR - Tracing route to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
goto Loop
还发现了一个很好的小Java应用程序,可以在这里为您打包


简单易用,完成任务:-)

非常感谢您的回答自Windows 7和Server 2018以来,我们有了
timeout
命令。谢谢@MicheldeRuiter,我怀疑我写这篇文章时没有进入Windows 7。感谢您的改进。非常感谢您的回答这行代码对我不起作用:for/F“usebackq tokens=1-4 delims=:。”%%G IN(
echo%time%
)如果%G%H GTR 1400 GOTO:EOF它必须是:for/F“usebackq tokens=1-4 delims=:。”%%G IN(
echo%time%
)如果%%G%%H GTR 1400 GOTO,请执行:使用-n-q 5进行EOFUsing路径设置可加快速度并提供详细的统计信息。或带-d选项的tracert。这会减少dns解析时间(每次解析都是毫无意义的…总是一样的),并提供更好的机会捕捉到上游的瞬时故障。当然,dns不会每次都是一样的。在我的一个网络上,DNS服务器通常是个问题!我也想这么做,但在lunux(Ubuntu18.04)中,有什么建议吗?谢谢你的脚本,看看它是否能抓住我目前遇到的问题。我做了一些修改:@echo off set Address=google.com set Delay=5 echo%date%%time%Ping%Address%每隔%Delay%seconds>>C:\pingtest\logfile.log:Loop Ping-n%Delay%127.0.0.1>nul echo%date%%time%Pinging%SystemRoot%\system32\Ping.exe-n 1%Address%\124;%SystemRoot%\system32\find.exe“TTL=”>NUL>>C:\pingtest\logfile.log如果%ERRORLEVEL%eq 0转到:循环回显%date%%time%Trace route%Address%>>C:\pingtest\logfile.log tracert%Address%>>C:\pingtest\logfile.log转到循环
@echo off
set Address=google.com
:Loop
PING -n 5 127.0.0.1>nul
echo Pinging %Address%
%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL >> C:\pingtest\logfile.log
if %ERRORLEVEL% EQU 0 goto :Loop
echo Trace route %Address% at %date% %time% >> C:\pingtest\logfile.log
tracert %Address% >> C:\pingtest\logfile.log
goto Loop
@echo off
set Address=www.google.com
set LogDir=C:\pingtest
md %LogDir%
%SystemRoot%\explorer.exe "%LogDir%"
echo PingTest script to monitor network connection.  Control-C to exit.
echo Tests connection by pinging %Address%.  Logs to %LogDir%\logfile.log.
echo %date% %time% Initial tracert (trace route) to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
:Loop
REM  5 second delay
PING -n 5 -w 1 127.0.0.1>nul
echo %date% %time% Pinging %Address%
echo %date% %time% Pinging %Address% >> %LogDir%\logfile.log
%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL
if %ERRORLEVEL% EQU 0 goto :Loop
echo %date% %time% PING ERROR - Tracing route to %Address%
echo %date% %time% PING ERROR - Tracing route to %Address% >> %LogDir%\logfile.log
tracert %Address% >> %LogDir%\logfile.log
goto Loop