Filtering Netstat output

Hi

I am running Windows XP and Windows 7 clients, with a Windows 2008 Server running an in-house app.

The application server is slowing down, and our developer team say it is because there are too many client connections originating from the same machines, i.e. the client machines are holding open connections instead of closing them with the server.

I'd like to go to some client machines, and find out how many connections they have to port 5000 (example) which the app uses.

I guess I need Netstat to do that? But how can I filter the Netstat output by either port or destination IP (the server IP address is 192.168.1.33)

Thanks in advance.
neil4933Asked:
Who is Participating?
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Of course you can do that, but since netstat output is consisting of lines of strings, it requires parsing like in cmd batches (which again is easier in PowerShell due to the availability of regular expressions). We would have to split a line like we do in the FOR loop, making it inconvinient. But I have seen PS code doing that, and it is (again) not difficult (see e.g. http://halr9000.com/article/599).

The strength of PowerShell is that it handles objects and their properties, not strings only. Using the method I showed is more versatile, as you can define the conditions to your liking:
[Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetActiveTcpConnections() | 
  Where-Object {$_.State -eq "Established"} |
  Where-Object {$_.RemoteEndPoint.Address -eq [IPAddress]"192.168.1.33"
           -or  $_.RemoteEndPoint.Port    -eq 443} |
  Group-Object {$_.RemoteEndPoint.Address} | 
  Select-Object @{name="Remote"; e={$_.Name}}, count]

Open in new window

[Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetActiveTcpConnections() | 
  Where-Object {$_.State -eq "Established"} |
  Where-Object {$_.RemoteEndPoint.Address -eq [IPAddress]"192.168.1.33"
           -and $_.RemoteEndPoint.Port    -eq 443} |
  Group-Object {$_.RemoteEndPoint.Address} | 
  Select-Object @{name="Remote"; e={$_.Name}}, count]

Open in new window

0
 
als315Commented:
netstat | findstr /c:"192.168.1.33"
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
To filter for e.g. port 443 (SSL), you would need to suppress the decoding of IP and port numbers, and to "find" the port:
netstat -n | find ":443	"

Open in new window

This is ending with a Tab character, else a port of 4431 aso. would be found, too. If you use
netstat -n | find /c ":443	"

Open in new window

you will get the count only.
But what you really want to have is a grouping by IP and port - and that cannot be done automatically in a batch (that easily). It would be easy to do in PowerShell, but I reckon you won't want to use that here. If you want a batch, you can start with:
@echo off
for /F "tokens=2-5 delims=: " %%A in ('netstat -n ^| find ":443	"') do @echo %%A,%%B,%%C,%%D

Open in new window

which just shows how to parse the output.
  %%A contains the local IP,
  %%B the local port,
  %%C the remote IP,
  %%D the remote port
If you change the sequence in echo, you can pipe the result to sort, and have it sorted for the corresponding value (local/remote IP/port).

You might find a tool like CurrPorts (http://www.nirsoft.net/utils/cports.html) or TcpView (http://technet.microsoft.com/en-us/sysinternals/bb897437) more handy if you want to perform analysis online and manually.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
neil4933Author Commented:
Hi All

Thanks for your responses...

"But what you really want to have is a grouping by IP and port - and that cannot be done automatically in a batch (that easily). It would be easy to do in PowerShell..."

Actually, I'd be very interested in how that worked with Powershell!!
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
[Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetActiveTcpConnections() | 
  Where-Object {$_.State -eq "Established"} |
  Where-Object {$_.RemoteEndPoint.Address -eq [IPAddress]"1.1.1.1" } |
  Where-Object {$_.RemoteEndPoint.Port -eq 443} | 
  Group-Object RemoteEndPoint | 
  Select-Object @{name="Remote"; e={$_.Name}}, count

Open in new window

will display the number of connections to 1.1.1.1:443 as an example.
But instead running on each client individually, I recommend to run it on the server and query LocalEndPoint.Port to get an overview over the connected clients:
[Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetActiveTcpConnections() | 
  Where-Object {$_.State -eq "Established"} |
  Where-Object {$_.LocalEndPoint.Port -eq 5000} | 
  Group-Object {$_.RemoteEndPoint.Address} | 
  Select-Object @{name="Remote"; e={$_.Name}}, count

Open in new window

0
 
neil4933Author Commented:
Thanks QLEMO.

Is it not possible to query the output of netstat directly from Powershell?

I was thinking of something like:

Get output of Netstat
Where destination address is 192.168.1.33 or
Where destination port is 443 (for example)
Where destination address is 192.168.1.33 *and* destination port is 443?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.