0

Does anyone knows how to change batch file/cmd background color using netstat command?

I'm actually monitoring some bank acquirers connections to a server with an auto-refreshing 'netstat' command.

title BANK #1
netstat -n 1 -an | find "IP_ADDRESS:PORT"
pause

And cmd echoes:

If connection is estabilished:

TCP    MY_SERVER_IP_ADD:LIST_PORT        REMOTE_IP_ADD:PORT    ESTABLISHED

If connection failed:

TCP    MY_SERVER_IP_ADD:LIST_PORT        REMOTE_IP_ADD:PORT    SYN_SENT

I need to know if is posible that background color changes depending on status, i mean, when status is ESTABLISHED, turn background Green. When status is SYN_SENT, turn background RED.

J. Fermin
  • 3
  • 3
  • 2
    The simplest idea would be to redirect the result from the `netstat` command to a file or a variable. Then perform another `find` using the string `ESTABLISHED`, and use the errorlevel from that to determine which `color` you wish to change to. – Compo Aug 22 '21 at 12:28
  • Well, it is possible to run `%SystemRoot%\System32\netstat.exe -a -n | %SystemRoot%\System32\findstr.exe /R "127\.0\.0\.1:1032.*127\.0\.\0\.1:1033.*ESTABLISHED" >nul`. If __FINDSTR__ finds a line matching that simple case-sensitive regular expression, the exit code is `0` and otherwise it is `1`. So there can be used as next line `if errorlevel 1 (color C0) else color 07`. Or there is used just one line like `%SystemRoot%\System32\netstat.exe -a -n | %SystemRoot%\System32\findstr.exe /R "127\.0\.0\.1:1032.*127\.0\.\0\.1:1033.*ESTABLISHED" >nul && color 07 || color C0`. – Mofi Aug 22 '21 at 16:46
  • Please see [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) for more information about the last line with the conditional operators `&&` and `||`. Please take also into account that the word `ESTABLISHED` is language dependent in output of __NETSTAT__. There is output on German Windows the GERMAN word `HERGESTELLT` instead of the English word `ESTABLISHED` as on English Windows. – Mofi Aug 22 '21 at 16:48

2 Answers2

0

I recommend to use the ip address of remote server to find string from connection's list. After string parsing you can get the connection state. Change color by connection status.

@echo off

REM define remote server IP
set "IP=8.8.8.8"

REM cycle's starting position
:loop

REM pause for 1 second
choice /c YN /t 1 /D Y>NUL

REM parsing connection list
for /f "tokens=4*" %%A in ('netstat -p tcp -na ^| find/i"%IP%"') do (

REM change background color to green
if "%%A"=="ESTABLISHED" color 27 

REM change background color to red
if "%%A"=="SYN_SENT" color 47
)

REM go to cycle's staring position
goto loop
Daemon-5
  • 241
  • 1
  • 6
0

Here is a versatile template based on your request but easily modified

Will be green or red as requested but off white for other conditions

@echo off & Title Bank#1
REM Change the following as required
set "LocalAddress=192.168"
set "ForeignAddress=Chromecast"


:MAIN
timeout 1 > nul
netstat -p tcp | find "%LocalAddress%" | find "%ForeignAddress%" | find "ESTABLISHED"
if %errorlevel%==0 color A0 & echo Still Established & goto main
netstat -p tcp | find "%LocalAddress%" | find "%ForeignAddress%" | find "SYN_SENT"
if %errorlevel%==0 color C0 & echo SYN_SENT & goto main
netstat -p tcp | find "%LocalAddress%"
color E0 & echo Check Incorrect Link Condition Between "%LocalAddress%" and "%ForeignAddress%"
goto Main

enter image description here enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36