2016-02-26 95 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