2016-02-26 92 views
2

我正在尝试创建一个批处理文件,它将不断ping通google.com并检查响应时间 - “time = Xms”。创建批处理文件,不断ping千谷歌并测试响应时间

  • 如果时间< = 39MS,平安(或背景)的文字应 是绿色的。
  • 如果时间> 40ms和< 80ms,该ping(或背景)的文本应变为橙色。
  • 如果时间> = 80ms,那么ping(或背景)的文本应该变成红色,即 。

我如果响应失败,此批此刻这坪谷歌每隔3秒从绿色到红色的背景:

@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 

这工作得很好,但我不知道如何测试响应时间。

+0

看看这个。它不会执行颜色更改位,但它具有评估ping响应时间的代码。 http://stackoverflow.com/questions/35591855/writing-a-batch-file-to-detect-ping-anomalies/35633613#35633613 –

回答

1

有一些怪癖。

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 
+0

太棒了!这正是我想要实现的。欢呼 – MajorHints

+0

如果你使用'set/a'来声明变量,它会像数字一样对待它们。 –

0

尝试这样的:

@echo off 
Title Echo Ping Reply 
mode con cols=57 lines=6 
::Choose how many seconds you must wait for the refresh 
Set MyPause=3 
:color 97 

:start 
CLS 
echo. 
ping www.google.com | findstr /i "TTL" 
Timeout /Nobreak /t %MyPause% > Nul 
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 
+0

嗨,感谢您的答复,但从我能理解这不检查数额时间为ping响应。我非常依赖我的ping(time = Xms),并且需要一种方法来检查它。目前看起来这只是检查ping是否发生,而不是发生多快/多慢(花费的时间)。我只是不知道如何将“time = X ms”放入一个变量中,以便它可以在if语句中使用。 – MajorHints

0

因为有时PowerShell不是一个选项,我想我会把一些东西拼凑在一起,并将其作为批处理脚本来完成。
Writing a batch file to detect ping anomalies
How to have multiple colors in a Windows batch file?

:: usage: badpings-color.bat [ip adress | hostname] 

@echo off 
set /a warnlimit=79 :: greater than this value is red 
        :: between the two values is yellow 
set /a goodlimit=39 :: less than or equal to this value is green 

if "%1"=="" (
    set pingdest=google.com 
    ) else (
    set pingdest=%1 
    ) 

echo Pinging %pingdest%. 
::echo Logging replies over %limit%ms. 
echo Press Ctrl+C to end. 

:Loop 
for /f "usebackq tokens=1-6" %%a in (`ping -n 1 %pingdest% ^| findstr "Request Reply request"`) do (
    set var=%%a %%b %%c %%d %%e %%f 
    set pingtimestr=%%e 
    ) 

if "%pingtimestr%"=="find" (
    echo Ping request could not find host %pingdest%. Please check the name and try again. 
    goto End 
    ) 
if "%pingtimestr%"=="host" (
    set /a pingtime=%warnlimit%+1 
    ) 
if not defined pingtimestr (
    set /a pingtime=%warnlimit%+1 
    ) 
if "%pingtimestr:~0,4%"=="time" (
    set /a pingtime=%pingtimestr:~5,-2% 
    ) 

if %pingtime% LEQ %goodlimit% (
    call :c 02 "[%time%] %var%" /n 
    goto EndOfLoop 
    ) 
if %pingtime% LEQ %warnlimit% (
    call :c 0E "[%time%] %var%" /n 
    goto EndOfLoop 
    ) 
if %pingtime% GTR %warnlimit% (
    call :c 0C "[%time%] %var%" /n 
    goto EndOfLoop 
    ) 

:EndOfLoop 
timeout /t 1 /nobreak >nul 
Goto Loop 

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
:: this section is from 
:: https://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-windows-batch-file/10407642#10407642 

:c 
setlocal enableDelayedExpansion 

:colorPrint Color Str [/n] 
setlocal 
set "s=%~2" 
call :colorPrintVar %1 s %3 
exit /b 

:colorPrintVar Color StrVar [/n] 
if not defined DEL call :initColorPrint 
setlocal enableDelayedExpansion 
pushd . 
': 
cd \ 
set "s=!%~2!" 
:: The single blank line within the following IN() clause is critical - DO NOT REMOVE 
for %%n in (^"^ 

^") do (
    set "s=!s:\=%%~n\%%~n!" 
    set "s=!s:/=%%~n/%%~n!" 
    set "s=!s::=%%~n:%%~n!" 
) 
for /f delims^=^ eol^= %%s in ("!s!") do (
    if "!" equ "" setlocal disableDelayedExpansion 
    if %%s==\ (
    findstr /a:%~1 "." "\'" nul 
    <nul set /p "=%DEL%%DEL%%DEL%" 
) else if %%s==/ (
    findstr /a:%~1 "." "/.\'" nul 
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%" 
) else (
    >colorPrint.txt (echo %%s\..\') 
    findstr /a:%~1 /f:colorPrint.txt "." 
    <nul set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%" 
) 
) 
if /i "%~3"=="/n" echo(
popd 
exit /b 

:initColorPrint 
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "DEL=%%A %%A" 
<nul >"%temp%\'" set /p "=." 
subst ': "%temp%" >nul 
exit /b 

:cleanupColorPrint 
2>nul del "%temp%\'" 
2>nul del "%temp%\colorPrint.txt" 
>nul subst ': /d 
exit /b 

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 

:End