【问题标题】:Manipulate cmd ping color based on time根据时间操作 cmd ping 颜色
【发布时间】:2016-02-07 22:55:12
【问题描述】:

我的互联网并不总是正常工作,我想根据 cmd windows 工具检查质量。我相信这是一项足够简单的任务。
我首先创建了一个快捷方式,以便可以轻松访问命令:

    C:\Windows\System32\PING.EXE 8.8.8.8 -t

现在我试图将 cmd ping 命令转换为基于输出的视觉响应命令。我想根据时间响应来改变颜色。
在寻找并没有找到任何相关的东西之后,我相信这要么是不可能的,要么没有人尝试过。

非常感谢:)
PD:(如果有什么不清楚的可以问,我很乐意回答)

【问题讨论】:

    标签: windows cmd ping


    【解决方案1】:

    根据 Magoo 的帖子,我编写了这个小批处理程序。 它询问目标、要发出的请求数、允许的最大时间和请求之间的时间,如果请求超过最大时间,则以红色打印,否则将请求数相加。它包括更准确的时间戳。

    复制并粘贴到一个文本文件中,并以扩展名“.bat”命名(但不要将其命名为“ping.bat”,否则程序将进入无限循环)。

    REM CMD PING TOOL
    REM By Daweb
    REM https://stackoverflow.com/users/3779294/daweb    
    
    @ECHO OFF
    
    REM Needed for Line colored
    SETLOCAL EnableDelayedExpansion
    FOR /F "tokens=1,2 delims=#" %%a IN ('"PROMPT #$H#$E# & echo on & for %%b in (1) do rem"') do (
      SET "DEL=%%a"
    )
    for /f %%a in ('copy /Z "%~f0" nul') do set "CR=%%a"
    
    
    ECHO *****************
    ECHO * CMD PING TOOL *
    ECHO *****************
    
    REM Start
    :start
    
    ECHO.
    ECHO Set yours values
    
    REM SET Target
    SET /p hostInput=" - Target (ip or hostname): "
    If "%hostInput%"=="" ECHO.&GOTO start
    
    REM SET loops
    SET /p loopsInput=" - Requests number: "
    SET /a loops=loopsInput
    
    REM SET time limit
    SET /p maxmsInput=" - Maximum Time Limit (ms): "
    SET /a maxms=maxmsInput
    
    REM Value used for sleep between loops
    SET /p sleepInput=" - Delay between requests (s): "
    SET /a sleepDelay=sleepInput+1
    
    REM Variables
    SET displayText=""
    SET /a countRequestsOk=0
    SET /a countRequestsKo=0
    SET /a totalRequests=0
    SET /a maxTime=0
    
    ECHO.
    ECHO START at %TIME% [target: %hostInput%, requests: %loops%, time limit: %maxms% ms, delay: %sleepInput% s]
    ECHO.
    
    
    REM Loop
    :loop
    
    REM Set time
    FOR /f "tokens=1-3 delims=/:" %%a IN ("%TIME%") DO (SET mytime=%%ah%%bm%%cs)
    
    REM Get ping value
    FOR /f "tokens=3delims==" %%a IN ('PING -n 1 %hostInput%') DO FOR /f "delims=m" %%b IN ("%%a") DO (
    
        SET /a timems=%%b
        SET /a totalRequests+=1
    
        REM Check result
        IF !timems! GTR %maxms% ( GOTO failed ) ELSE (  GOTO success )
    
    )
    
    
    REM Request success
    :success
    SET /a countRequestsOk+=1
    IF !timems! GTR !maxTime! ( SET /a maxTime=timems )
    <nul set /P "=!countRequestsOk! requests [Max !maxTime! ms]!CR!"
    
    GOTO next
    
    
    REM Request failed
    :failed
    IF !countRequestsOk! GTR 0 ECHO.
    
    SET /a countRequestsOk=0
    SET /a countRequestsKo+=1
    
    SET displayText=" %mytime% - !timems!ms"
    CALL :ColorText 0c !displayText!
    
    GOTO next
    
    
    REM Next loop
    :next
    
    REM Sleep a little bit
    IF %sleepDelay% GTR 1 ( ping -n %sleepDelay% localhost > nul )
    
    REM Check continue
    SET /a loops-=1
    IF %loops% gtr 0 GOTO loop
    
    
    REM Display result
    IF !countRequestsOk! GTR 0 ECHO.
    ECHO.
    ECHO STOP at %TIME%
    ECHO.
    
    if !countRequestsKo! GTR 0 (
        SET displayText="FAILED - !countRequestsKo! requests over %maxms% ms on !totalRequests! requests in total"
        CALL :ColorText 0c !displayText!
    ) ELSE (
        SET displayText="SUCCESS - No request over %maxms% ms on !totalRequests! requests in total"
        CALL :ColorText 02 !displayText!
    )
    
    
    REM Ask if restart
    ECHO.&ECHO *********************
    SET /p restartInput="Do it again ? (Y/N): "
    If "%restartInput%"=="" ECHO *********************&GOTO start
    If /I "%restartInput%"=="y" ECHO *********************&GOTO start
    If /I "%restartInput%"=="n" ECHO *********************&GOTO end
    
    
    REM End
    :end
    PAUSE
    GOTO :EOF
    
    
    REM Line color
    :ColorText
    ECHO off
    ECHO %DEL% > "%~2"
    FINDSTR /v /a:%1 /R "^$" "%~2" NUL
    DEL "%~2" > NUL 2>&1
    

    【讨论】:

    • 非常感谢!这正是我想要的!现在已经过去了一段时间,我发现了一些可以做的改进,但由于我有更多的编程经验,我可能会自己试一试。再次感谢您的支持和清晰的解释!
    【解决方案2】:
    @ECHO OFF
    SETLOCAL
    SET loops=10
    :loop
    FOR /f "tokens=3delims==" %%a IN ('PING 8.8.8.8 -n 1') DO FOR /f "delims=m" %%b IN ("%%a") DO ECHO %%b&COLOR %%b&GOTO cchgd
    :cchgd
    PAUSE
    SET /a loops-=1
    IF %loops% gtr 0 GOTO loop
    COLOR
    
    GOTO :EOF
    

    一个简单的演示 - 重复 ping 10 次,根据响应改变颜色。随心所欲地操作...

    【讨论】:

    • 我根本不使用那种表示法......你能给我一个参考,在哪里学习如何编辑每个变量,或者如果这太乏味了,我会很感激有一个功能代码。谢谢! :)
    【解决方案3】:

    我不确定我是否知道所需的输出应该是什么,但这会在响应时间 0-39 毫秒时输出绿色文本,在 40-79 毫秒时输出黄色文本,在 80 毫秒以上时输出红色文本。

    使用以下命令从 cmd.exe 提示符运行此程序,或将其放入 .bat 文件脚本中。将目录更改为 Get-PingColor.ps1 文件所在的位置。

    powershell -NoLogo -NoProfile -File "%USERPROFILE%\bin\Get-PingColor.ps1"
    

    === Get-PingColor.ps1

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string[]]$ComputerNames
    
        ,[Parameter(Mandatory=$false)]
        [int]$Count = 4
    
        ,[Parameter(Mandatory=$false)]
        [int]$SpeedMinimumSlow = 80
    
        ,[Parameter(Mandatory=$false)]
        [int]$SpeedMinimumMedium = 40
    )
    foreach ($ComputerName in $ComputerNames) {
        $Pings = Test-Connection -ComputerName $ComputerName -Count $Count
        $Average = ($Pings | Measure-Object -Property responsetime -Average).Average
        $ForegroundColor = 'Green'
        if ($Average -ge $SpeedMinimumSlow) { $ForegroundColor = 'Red'}
        else { if ($Average -ge $SpeedMinimumMedium) { $ForegroundColor = 'Yellow' }}
    
        Write-Host -ForegroundColor $ForegroundColor -BackgroundColor 'Black' "$ComputerName $Average ms"
    }
    

    === 执行示例

    我不愿意将图像放入帖子中,但我看不到在 SO 上产生颜色的方法。

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 2014-05-22
      • 2011-12-23
      • 2013-06-29
      相关资源
      最近更新 更多