简体   繁体   中英

Windows, how to handle return value from netstat? ( batch / .bat )

This is my code :

@echo off
netstat -a -n | find /c "127.0.0.1:80"
pause

It return 1 Value in my CMD. i want to make IF Condition, for example, if The return value is 1 do this, if 0 do this. can you help me guys?

Wrap the command in a for /f parsing the output.

@echo off
For /f %%A in ('netstat -a -n ^| find /c "127.0.0.1:80"') Do Set Count=%%A
If %Count% equ 0 (
  echo Count = 0 do this
) Else (
  echo Count not 0 do that
)
pause

You could probably just use this construct:

NetStat -na | Find "127.0.0.1:80" >Nul && (
    Echo Found
) || (
    Echo Not found
)

Change Echo Found to the command(s) you need for one or more matches and Echo Not found to the command(s) you need for no connection matches a little like this:

Depending upon your specific requirements you could possibly replace -na with -np TCP

BTW your script is returning the value from find not from netstat .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM