Search code examples
windowsbatch-filecmd

Change cmd background color depending of netstat connection status


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.


Solution

  • 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