在记事本中Ping结果

时间:2019-12-14 15:31:50

标签: cmd ping

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

.
├── Dockerfile
├── README.md
├── app.yaml
├── credentials    # <- credential file is in this directory
├── node_modules
├── package-lock.json
├── package.json
├── public
└── src
    ├── App.css
    ├── App.js
    ├── App.test.js
    ├── Main.js
    ├── components
    │   ├── AddFile.js
    │   ├── Footer.js
    │   ├── Header.js
    │   ├── Inner.js
    │   ├── Login.js
    │   ├── PaperFolder.js
    │   ├── PaperInfo.js
    │   ├── PaperLabels.js
    │   ├── SearchResultTable.js
    │   ├── SideBar.js
    │   ├── Signup.js    # <- the code above
    │   ├── TopLinks.js
    │   └── ToppageMain.js
    ├── images
    │   └── maarten-van-den-heuvel-8EzNkvLQosk-unsplash.jpg
    ├── index.css
    ├── index.js
    ├── serviceWorker.js

2 个答案:

答案 0 :(得分:0)

您的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

编辑以获取注释中的其他要求:

@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

答案 1 :(得分:0)

持续将Internet连接状态输出到文本文件和主机。

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