Kill TCP IP & Port

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

example;-
netstat -a output is

Active Connections

  Proto    Local Address                        Foreign Address                State
  TCP       192.168.1.101:49366            192.168.1.55:65000          ESTABLISHED
  TCP       192.168.1.101:49475            192.168.1.55:64000          ESTABLISHED
  TCP       192.168.1.101:49384            192.168.1.55:63500          ESTABLISHED
  TCP       192.168.1.101:49396            192.168.1.55:22                 ESTABLISHED

Need to forcefully kill all TCP connection which is connected with 192.168.1.55
LVL 1
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.

JustInCaseCommented:
netstat -ano
output:
Proto  Local Address          Foreign Address        State           PID
  TCP    0.0.0.0:135            0.0.0.0:0           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 http://www.nirsoft.net/utils/cports.html 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 * * 192.168.1.55 *

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:

[TCPKill.bat]
@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:
Helpful
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.