SQLServerCentral Article

Find the Port a Connection is Using

,

I would imagine that most of us in the SQL Server universe read the registry to validate or find the port(s) that SQL Server is listening on. I had a situation that was a bit different. I needed to determine on which port(s) connections were coming through from the client to the database server..

We have vendors that do work for my company via VPN. These vendors access the SQL Servers through specific ports in order to distinguish their connections from our internal (employee) connections. I had to provide a quick report on the vendor connections and for some reason completely missed the system view that has the connection information in it. So what did I do? I used Google and Bing to try to find an answer. No joy there. I guessed that this isn't a normal operation so I knew I had to post the question on SSC and had my answer in short order! Thanks Steve.

This is the script. It is short and simple but what I needed. The views have a wealth of information in them so more than just a port number for a connection is available. Hope it this helps someone else.

SELECT c.session_id, c.local_tcp_port, s.login_name, s.host_name, s.program_name
FROM sys.dm_exec_connections AS c INNER JOIN
sys.dm_exec_sessions AS s on c.session_id = s.session_id
WHERE c.local_tcp_port <> 1433

If you run this you will get some results, similar to the samples shown below.

session_id local_tcp_port login_name host_name program_name

88 11433 DomainXX\vendor1 D0004785 .Net SqlClient Data Provider
304 2048 DomainXX\vendor2 Laptop123 .Net SqlClient Data Provider
1022 2096 DomainXX\vendor3 MRE23098 Microsoft SQL Server Management Studio - Query

I save the output to a csv file since this is used internally to audit external connections. Since we use it (DBA group), it doesn't need to be a "formal" report but the query could be used in a data source for SSRS and the report could be "spiffed up" if it were to go to management.

A side benefit of needing this data is that it gave me another tool to use as part of my process for tracking down server performance issues. When I get a call about performance on one of the servers where vendors have direct access to the databases, I'll run this query first. While our vendors are quite good on the engineering side, their queries are somewhat "lacking" 😉

I hope it this helps or gives you some ideas.

Rate

4.79 (38)

You rated this post out of 5. Change rating

Share

Share

Rate

4.79 (38)

You rated this post out of 5. Change rating