根据结果​​更改命令提示符中的颜色? [英] Change Color in Command Prompt Based on Result?

查看:91
本文介绍了根据结果​​更改命令提示符中的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有点长.

如果我想运行一个连续的ping命令,如果说结果超出某个点,是否可以将输出用其他颜色列出?

If I want to run a continuous ping command, is it possible to have the output listed in a different color if lets say the result is beyond a certain point?

示例:

Ping结果返回22ms,白色 Ping结果返回300+红色

Ping result comes back with 22ms, white Ping result comes back with 300+, red

那完全有可能吗?我需要改成一个批处理文件吗?

Is that at all possible? Will I need to make a batch file instead?

下面列出的脚本非常有效.但是,我需要采取一些其他措施.当IP地址超时时,此脚本将不执行任何操作.它只是在等待下一次ping.反正有整合吗?

The script listed below works REALLY well. However, I need some additional action with it. When an IP address times out, this script does nothing. It just sits waiting for the next ping. Is there anyway to incorporate that?

推荐答案

一个基本的解决方案可能就是类似的东西.

A rudimentary solution would be something along the lines of.

  @setlocal enableextensions enabledelayedexpansion
@echo off
set /p ipaddr="Enter ip or url: eg localhost or google.co.za or 192.168.0.1 "
set /p cutoff="enter minimum good reply ms: eg 300 "
set /a res = 0 - 1 
:loop
for /f "tokens=7,9" %%a in ('ping -n 1 !ipaddr!') do (
set /a res = 0 - 1 
  if "%%a"=="Average" (
  set a=%%b
  set /a res=!a:~0,-2!
  )
)

IF %res% == -1 (
  COLOR 2
  echo %ipaddr% failed to respond in time
)
IF %res% LEQ %cutoff% (
    IF %res% NEQ -1 (
        COLOR 2
        echo %ipaddr% responded in %res%ms
    )
)else (
    COLOR 4
    echo %ipaddr% responded in %res%ms
)
ping -n 3 127.0.0.1 >nul: 2>nul:
goto :loop

endlocal

将其保存到.cmd文件,然后双击它

Save it to a .cmd file and then just double click it

这篇关于根据结果​​更改命令提示符中的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆