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

发布于 2024-11-26 03:37:17 字数 483 浏览 1 评论 0原文

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

我一直在尝试遵循此处发表的评论:

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

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

从互联网上的任何地方我都会失去与家庭网络的连接。在工作中,我开始执行 ping 操作,当它失败时,我执行了跟踪路由,但在到达我的 IP 之前它就失败了。

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

I want to continuously ping my home public IP address, and if the ping fails automatically do a traceroute to see where it's failing.

I've been trying to follow the comments made here:

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

Windows compatible would be preferable, bat or vbs would be best.

From anywhere on the internet I will lose my connection to my home network. From work I have started a ping and when it drops I've done a traceroute and it fails before it gets to my IP.

I need a log file to prove that it is not my modem, or router, or computer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

初与友歌 2024-12-03 03:37:17
@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。
我选择使用 "%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL 查找 TTL 的无结果,而不是查找错误级别 1 “

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

谢谢大家!

@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

This is what I ended up going with, if anyone else ever needs this. Essentially the "Ping -n 127.0.0.1>Nul" is to add a 5 second counter so that it only pinged the destination every 5 seconds, 5 can be changed to whatever value is needed.

Windows 7 has this problem where a ping may result with something like "reply from 192.168.1.5: Destination host unreachable". So instead of erroring out it gets a reply from itself and not the error level 1.
Instead of looking for Error Level 1 I choose to look for no result for TTL with "%SystemRoot%\system32\ping.exe -n 1 %Address% | %SystemRoot%\system32\find.exe "TTL=" > NUL"

Anyway, I'm sure the other answers here were very similar and may have worked, so I am ranking them up, but marking this as the answer.

Thanks all!

短叹 2024-12-03 03:37:17
@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
@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
伴随着你 2024-12-03 03:37:17

您可以制作一个简单的批处理文件来尝试 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

这里有很大的改进空间。

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

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

choice /d y /t 5 > nul

You could make a simple batch file that tries a ping and if it fails does a tracert, eg:

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

There's plenty of scope for refinement here.

Then create a scheduled task that runs it every five minutes or whatever suits you.

Alternatively you could include a loop with a 'sleep' in it. There's a poor man's sleep at Sleeping in a batch file that uses:

choice /d y /t 5 > nul
凉城已无爱 2024-12-03 03:37:17
: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 地址时才会出现),则执行 Tracert 。 PING 中的 -n-w 开关告诉它只跳转一次,并在没有得到响应的 1 秒后超时。如果您正在 ping 本地主机,这完全没问题。第二个 FOR 语句是有一个停止点。将 1400 更改为您希望脚本停止的时间(当然是军事时间)。

: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

Basically, this states do ping, if it finds a line that has an instance of the word Request (which only appears if you can't ping the address) perform a tracert. The -n and -w switches in PING tell it to jump only once and timeout after 1 second of not getting a response. This is perfectly fine if you are pinging your localhost. The second FOR statement is to have a stopping point. Change the 1400 to a time you wish for the script to stop (in military time of course).

不再见 2024-12-03 03:37:17

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

简单易用,功能强大:- )

I have just been looking for the same thing to investigate why a VPN keeps dropping on a wired connection, used one of the batch file suggestions above which was great.
Also found a nice little Java App which packages it for you here
Internet Connectivity Monitor

Simple to use and does the job :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文