0

I am looking for a simple batch script that kills a process which listens to a certain ip and to a certain port. If there are less than 10 connections to this process, then it should be killed. If there are more than 10 established connections, the process should not be killed. (it can count the number of lines from -ano output and if there are less than 10 lines it should kill it)

I am currently using a simillar script but for a different task: Batch file that kills a certain process but instead of increasing it should decrease connections number and kills the process if there are less than x connections to that ip:port. Connection state in this case doesn't matter, it just has to count the number of connections only.

Chris
  • 107
  • 2
  • 8
  • It looks like an easy edit on my previous batch: remove the "CLOSE_WAIT" filter, change the greater-than to a lesser-than. Do you really need help with that? (btw: it wasn't me who downvoted your question) – pgr Feb 24 '15 at 13:30
  • I was really hoping to get an answer from you @pgr since you were the one that helped me out with the script itsel so thanks for being active once more. I have tried to change c+1 to c-1 and it's not working. Also, I don't see a greater-than symbol to change it to lesser-than (don't worry about devote, probably who did it had a good reason for this but if I was an expert I wouldn't have asked for help here and I really suck when it comes to .bat files) – Chris Feb 24 '15 at 13:35
  • Would appreciate if you could help me out again,whenever you get some spare time to modify the script you created in the link above to meet my conditions (I'm really a newbie when it comes to batch fiels). Thanks and I'm still using the first script you helped me out and working like a charm since then. – Chris Feb 24 '15 at 13:36
  • NO, the c still needs to add so it can count. It's the IF condition that needs to change. The operator is `geq` for `greater or equal then`, I'll change it to `lss`for `lesser than`. – pgr Feb 24 '15 at 13:41
  • Yes, I am that bad when it comes to batches. Thanks for taking the time to help me once more. – Chris Feb 24 '15 at 14:04

1 Answers1

0

Try this:

echo off

set /a c=1
setlocal ENABLEDELAYEDEXPANSION

FOR /F "tokens=5 delims= " %%I IN (
    'netstat -ano ^| find "127.0.0.1:5900"'
) DO (
    set /a c=c+1
    set /a last=%%I
)
if %c% lss 10 (
   taskkill /PID !last!
)

endlocal
pgr
  • 459
  • 5
  • 16