【问题标题】:Ping results in notepad在记事本中 Ping 结果
【发布时间】:2019-12-14 15:31:50
【问题描述】:

谁能告诉我这段代码有什么问题,我试图捕捉链接失败的日期和时间,否则它会报告链接正在工作,或者如果它显示 ping 响应会更好。

@echo off
echo Internet Testing by RN Choudhury.
:loop
ping 8.8.8.8 -n 1 -w 60 > nul
if errorlevel 1 echo %date% - %time% Not connected >> InternetFail.txt else 
echo Link is working
ping -n 30 127.0.0.1 > nul
goto loop

【问题讨论】:

    标签: cmd ping


    【解决方案1】:

    不断输出网络连接状态到文本文件和主机。

    Windows 10 64 位。不需要管理员权限。

    CMD 批处理脚本使用 ping、if/else、echo、重定向和 goto 循环将 Internet 连接状态持续输出到文本文件和主机。

    Ctrl+C 跳出循环。

    @title Output internet connection status to text file and host.
    @rem Windows 10 64-bit. Does not require admin privileges.
    @echo off 
    setlocal enableextensions 
    cd.> InternetFail.txt 
    :loop
    SET errorlevel=
    ping -n 2 8.8.8.8 -w 60 | find "TTL=" > nul
    REM ping -n 30 8.8.8.8 -w 60 | find "TTL=" > nul
    if errorlevel == 1 (
    (
    ECHO. 
    ECHO  %date% - %time% INTERNET DOWN
    ping -n 2 8.8.8.8 -w 60
    REM ping -n 30 8.8.8.8 -w 60
    )>> InternetFail.txt 
    ECHO. 
    ECHO    Ctrl+C to break out of loop. 
    ECHO.
    ECHO  %date% - %time% INTERNET DOWN
    ping -n 2 8.8.8.8 -w 60
    REM ping -n 30 8.8.8.8 -w 60
    ) else (
    ECHO. 
    ECHO    Ctrl+C to break out of loop. 
    ECHO.  
    ECHO %date% - %time%  Link is working.
    ping -n 2 127.0.0.1
    REM ping -n 30 127.0.0.1
    )
    goto :loop
    exit /b 
    

    【讨论】:

    • 嗨,谢谢,第 1 个是我想要的类型,但我只想捕获失败时间,如果可能的话(也有失败的原因),当链接存在时,我想在批处理文件屏幕。比如来自 8.8.8.8 的回复:bytes=32 time=27ms TTL=55 来自 8.8.8.8 的回复:bytes=32 time=38ms TTL=55 来自 8.8.8.8 的回复:bytes=32 time=33ms TTL=55 来自 8.8 的回复。 8.8:bytes=32 time=32ms TTL=55 来自 8.8.8.8 的回复:bytes=32 time=28ms TTL=55 来自 8.8.8.8 的回复:bytes=32 time=28ms TTL=55 来自 8.8.8.8 的回复:bytes=32 time=31ms TTL=55 来自 8.8.8.8 的回复:bytes=32 time=29ms TTL=55
    • @rab 更新。如果这个答案有用,你应该点赞。
    【解决方案2】:

    您的if 语法有缺陷:

    if errorlevel 1 (echo %date% - %time% Not connected >> InternetFail.txt) else echo Link is working
    

    或者为了更好的可读性:

    if errorlevel 1 (
      echo %date% - %time% Not connected >> InternetFail.txt
    ) else (
      echo Link is working
    )
    

    注意:ping 可能会给出误报(Reply from <localhost>: destination not reachable 在技术上响应,所以errorlevel 将为零。

    更好的使用:

    ping 8.8.8.8 -n 1 -w 60 | find "TTL=" > nul
    

    编辑了解 cmets 中的其他要求:

    @echo off
    setlocal 
    set "logfile=InternetFail.txt"
    set "host=8.8.8.8"
    :loop
    for /f "delims=" %%a in ('ping -n 1 -w 60 %host% ^|findstr /r "TTL= \.$"') do (
      echo %%a|find "TTL=" >nul && (
        >>"%logfile%" echo %date% %time% Link is up:   %%a
        echo %date% %time% Link is up:   %%a
      ) || (
        >>"%logfile%" echo %date% %time% Link is up:   %%a
        echo %date% %time% Link is up:   %%a
      )
    )
    timeout 30 >nul
    goto :loop
    

    【讨论】:

    • 谢谢。它工作正常。但正如我所提到的,我想在命令提示符窗口中显示结果,并且还想在记事本文件中捕获它们。现在详细信息正在记事本文件中捕获,但它们不会显示在命令提示符中。同样在我之前的批处理文件中,我可以通过 ping 到本地主机来设置时间限制,现在在这个批处理文件中,我该如何设置时间限制。如果我想像这样每分钟或 30 秒测试一次互联网。提前致谢
    • 现在好点了吗?如果脚本应该在 XP 或更低版本上运行,请将 timeout 命令替换为您的 ping 解决方法(timeout 在 XP 中不可用)。
    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多