根据时间操纵cmd ping颜色

时间:2016-02-07 22:55:13

标签: windows cmd ping

我的互联网并不总是正常工作,我想根据cmd windows工具检查质量。我相信这是一个足够简单的任务来处理它 我已经开始制作一个快捷方式,以便我可以轻松访问该命令:

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

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

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

3 个答案:

答案 0 :(得分:0)

@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次,根据响应改变颜色。操纵你想做的事情......

答案 1 :(得分:0)

根据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 :(得分:0)

我不确定我是否知道所需的输出应该是什么,但这将在响应时间为 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 上产生颜色的方法。 localhost example enter image description here