我正在尝试创建一个批处理文件,该文件将不断ping google.com并检查响应时间-“ time = Xms”。
如果时间是绿色的。
如果时间> 40毫秒且背景)应该变成橙色。
如果时间> = 80ms,则该ping(或背景)的文本应
变红。
目前,我有这批处理,如果响应失败,每3秒ping google会将背景从绿色更改为红色:
@echo off
:color 97
:start
PING -n 1 www.google.com
call :color
goto :start
:color
IF %ERRORLEVEL% EQU 0 (
COLOR 27
) else (
COLOR 47
ping -n 1 127.0.0.1 >nul
COLOR 74
ping -n 1 127.0.0.1 >nul
COLOR 47
)
ping -n 3 127.0.0.1 >nul
GOTO:EOF
这工作正常,但我不知道如何测试响应时间。
最佳答案
有一些怪癖。
a)您必须将ping
的期望值赋给变量。使用for
进行获取。
b)您不能直接比较它,因为if
比较字符串而不是数字(2
大于10
)。在字符串中添加前导零(然后将其剪切为固定长度)
c)cmd
没有为单行(或字符)着色的本地方法。可以使用纯cmd
完成,但是我认为powershell
是一种更好的方法。
@echo off
:loop
set "tim=unreachable"
for /f "tokens=7 delims== " %%i in ('PING -n 1 www.google.com ^|find "TTL"') do set "tim=%%i"
set "ti=0000%tim%"
set "ti=%ti:~-6,-2%"
if %ti% leq 0040 powershell write-host -foreground green %tim% & goto :loop
if %ti% leq 0080 powershell write-host -foreground yellow %tim% & goto :loop
powershell write-host -foreground red %tim% & goto :loop
关于batch-file - 创建一个可连续ping谷歌并测试响应时间的批处理文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35654701/