创建批量文件,不断ping谷歌并测试响应时间

时间:2016-02-26 14:46:28

标签: batch-file testing cmd ping

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

  • 如果时间< = 39ms,该ping(或背景)的文本应该 是绿色的。
  • 如果时间> 40ms和< 80ms那个ping的文本(或者 应该变成橙色。
  • 如果时间> = 80ms,该ping(或背景)的文本应该 变成红色。

我现在有这个批次,如果响应失败,每3秒ping一次谷歌会将背景从绿色变为红色:

    @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

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

3 个答案:

答案 0 :(得分:1)

有一些怪癖。

a)您必须为变量获得所需的/值。使用ping获取它。

b)您无法直接对其进行比较,因为for会比较字符串,而不是数字(if大于2)。将前导零添加到字符串中(然后将其剪切为固定长度)

c)10没有为单行(或字符)着色的原生方式。它可以使用纯cmd来完成,但我认为cmd是一种更好的方法。

powershell

答案 1 :(得分: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

答案 2 :(得分:0)

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

<html>
    <head>
        <body>
            <div style="background:black;width:75px;height:150px;margin:auto;">
                <div id="redlight" style="background:#ff0000;width:40px;height:40px;border-        radius:40px;margin:auto;"></div>
                <div id="yellowlight" style="background:#3F4A00;width:40px;height:40px;border-radius:40px ;margin:auto"></div>
                <div id="greenlight" style="background:#044A00;width:40px;height:40px;border-radius:40px;margin:auto"></div>
                <button onclick="start()" style="width:75px;height:30px;margin:auto">Motion- Start!</button>
            </div>
            <script>
                var redlight = document.GetElementById(redlight);
                var yellowlight = document.GetElementById[yellowlight];
                var greenlight = document.GetElementById[greenlight];
                var colors = ['#ff0000', '#520202', '#ffff00', '#3F4A00', '#008000', '#044A00']

                function start()
                if redlight.style.background == colors[0] {
                    redlight.style.background = colors[1] //switch off red
                    yellowlight.style.background = colors[2] //switch on yellow
                } else if (yellowlight.style.background == "yellow") {
                    yellowlight.style.background = colors[3] //switch off yellow
                    greenliight.style.background = color[4] //switch on green   
                } else
                if (greenlight.style.background == "green") {
                    greenlight.style.background = colors[5] //switch off green
                    redlight.style.background = colors[0] //switch on red
                }

            </script>
    </head>
    </body>