根据时间操作 cmd ping 颜色

Manipulate cmd ping color based on time

我的网络不是一直正常,我想用 cmd windows 工具检查网络质量。我相信这是一个足够简单的任务来处理。
我首先创建了一个快捷方式,这样我就可以轻松访问命令:

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

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

非常感谢:)
PD:(如果有任何不清楚的地方,请提问,我很乐意回答)

@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 次,根据响应改变颜色。随心所欲地操纵...

基于Magoo的post,我写了这个批处理小程序。 它询问目标、要发出的请求数、允许的最长时间以及请求之间的时间,然后如果请求超过最大时间则以红色打印,否则它对请求数求和。它包括更准确的时间戳。

复制粘贴一个文本文件,命名为“.bat”(但不要命名"ping.bat"否则程序会进入死循环)

REM CMD PING TOOL
REM By Daweb
REM https://whosebug.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

我不确定我是否知道所需的输出应该是什么,但这将在响应时间 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"
}

=== 执行示例

我讨厌将图像放入 post,但我看不到在 SO 上生成颜色的方法。