10

我想连续 ping 我的家庭公共 IP 地址,如果 ping 失败自动执行 traceroute 以查看失败的位置。

我一直在尝试关注这里的评论:

http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/efc97c66-60a6-4fd7-8be4-4b454d040ce5

Windows 兼容会更好,bat 或 vbs 最好。

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

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

4

5 回答 5

12
@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

如果其他人需要这个,这就是我最终要做的。本质上,“Ping -n 127.0.0.1>Nul”是添加一个 5 秒计数器,以便它仅每 5 秒 ping 一次目标,5 可以更改为所需的任何值。

Windows 7 存在这个问题,其中 ping 可能会导致类似“从 192.168.1.5 回复:无法访问目标主机”。因此,它不会出错,而是从自身获得回复,而不是错误级别 1。我没有寻找错误级别 1,而是选择使用 "%SystemRoot%\system32\ping.exe -n 1 %Address 寻找 TTL 的无结果% | %SystemRoot%\system32\find.exe "TTL=" > NUL"

无论如何,我确信这里的其他答案非常相似并且可能有效,所以我将它们排名,但将其标记为答案。

谢谢大家!

于 2011-07-26T22:31:16.963 回答
5
@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
于 2014-08-11T21:00:42.103 回答
4

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

setlocal
set host=www.bigpond.com
set logfile=nettest.log
echo %date% %time%>>%logfile%
ping %host%>>%logfile%
if ERRORLEVEL 1 tracert %host%>>%logfile
endlocal

这里有很大的改进空间。

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

或者,您可以在其中包含一个带有“睡眠”的循环。在一个批处理文件中的 Sleeping 中有一个穷人的睡眠,该文件使用:

choice /d y /t 5 > nul
于 2011-07-26T04:40:00.103 回答
2
:LOOP
FOR /F "usebackq tokens=1" %%F IN (`ping localhost -n 1 -w 1 ^| find "Request"`) DO (
  IF "%%F"=="Request" (
    tracert localhost
  )
)>>log.txt
FOR /F "usebackq tokens=1-4 delims=:." %%G IN (`echo %time%`) DO IF %G%H GTR 1400 GOTO:EOF
GOTO LOOP

基本上,ping如果它找到具有单词实例的行Request(仅在您无法 ping 地址时出现),则此状态会执行跟踪。和开关告诉它只跳转一次并在没有得到响应的 1 秒后超时-n。如果您正在 ping 本地主机,这非常好。第二个陈述是要有一个停止点。将 更改为您希望脚本停止的时间(当然是军用时间)。-wPINGFOR1400

于 2011-07-26T17:31:01.070 回答
0

我一直在寻找同样的东西来调查为什么 VPN 在有线连接上不断掉线,使用了上面的批处理文件建议之一,这很棒。还找到了一个不错的小 Java 应用程序,它在这里为您打包 Internet 连接监视器

易于使用并且可以完成工作:-)

于 2016-11-11T12:05:01.167 回答