Kill TCP IP & Port

Hi Experts,
How to grep connected tcp IP & Port connection then forcefully kill using batch.

netstat -a output is

Active Connections

  Proto    Local Address                        Foreign Address                State
  TCP            ESTABLISHED
  TCP            ESTABLISHED
  TCP            ESTABLISHED
  TCP                   ESTABLISHED

Need to forcefully kill all TCP connection which is connected with
Rajat SehgalFounder Enhance TechnologyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

netstat -ano
Proto  Local Address          Foreign Address        State           PID
  TCP             ESTABLISHED      1296

Open in new window

taskkill /f /im 1296

It may require elevated privileges.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Justin's answer is for killing the process listening or being connected to a particular port or IP, but certainly that is not what you are after - in particular as that is still a manual process. And even if only a single process is concerned, I would use either SysInternals TcpView or the following tool:

At you can find a tool called CurPorts, which mainly is been used to show connections with their associated processes in a GUI. But it also allows a commandline kill:
   curports /close localIP localport remoteIP remoteport process
where you can omit the process (name or ID), and use * for the other parameters. That is:
   curports /close * * *

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
Is there any idea with batch, which can i use easily ?
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
All you need to put into a .cmd or .bat file is the line as I have shown (the very last line). Put that and CurPorts into the same folder, and run the batch file. Nothing more to do.
Giovanni HewardCommented:
If you're trying to kill all connections from the command line then simply run

cports /close * * * *

Open in new window

If you want to process each connection for some reason, then you could use the following as a template:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=2,3,5" %%a in ('netstat -ano -p tcp^|findstr ESTABLISHED') do (
	set str=%%a %%b %%c
	set str=!str::= !
	if exist cports.exe (
		echo running cports.exe /close !str!
		cports.exe /close !str!
	) else (
		echo cports.exe not found.
		goto :eof
	set str=

Open in new window

Which is good for IPv4 connections.  Bear in mind a given process may simply try to reconnect once disconnected.
Rajat SehgalFounder Enhance TechnologyAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.