How can i see what IP address made the request to SQL Server?
Asked Answered
C

7

7

i need to clearly see what IP address made what sql query to SQL server. I am trying to use SQL Profiler, but it seems there is no way i can somehow to differentiate the machine (browser) where the query came from. It only has the communication details between the web server and the sql server. Is there ANY way for me (any unknown log?) that will allow me to see the identification of the original machine where the query came from?

Thanks for any suggestion.

HF

Clareta answered 4/11, 2009 at 15:46 Comment(1)
ok.. yes.. thanks for confirmation (unless i send the ip explicitly to db, there is no way to say). thanks you.Clareta
T
6

You can get the hostname of the current connection, or really any information from the sysprocesses table

SELECT hostname FROM sys.sysprocesses WHERE spid = @@SPID

This obviously won't work to get the ip address of web hosts if that's what you're looking for.

Toni answered 4/11, 2009 at 16:0 Comment(0)
S
4

Like @joshperry said, you can retrieve client address and server address with

SELECT client_net_address, local_net_address 
FROM sys.dm_exec_connections 
WHERE session_id = @@SPID
Stood answered 26/9, 2016 at 14:11 Comment(0)
V
3

As far as SQL Server is concerned the request will always come from your webserver. You need to capture and log the IP address in your web app.

Vasoinhibitor answered 4/11, 2009 at 15:51 Comment(0)
I
1
SELECT r.client_net_address,sqltext.Text
  FROM sys.dm_exec_requests req left join sys.dm_exec_connections as r on req.session_id=r.session_id
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
Illation answered 8/3, 2017 at 10:10 Comment(2)
Your answer certainly is worth a little explanation. Kindly refer to stackoverflow.com/help/how-to-answer .Quarto
Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how and why this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.Amedeo
F
0

If you do not write code in the application to pass the client address from the web server to the database you will have to look at the web logs - they will give the ip address of the client.

Fluidize answered 4/11, 2009 at 15:50 Comment(0)
A
0

SELECT * FROM sys.dm_exec_connections returns information about the connections established to this instance of SQL Server and the details of each connection (https://msdn.microsoft.com/en-us/library/ms181509%28v=sql.120%29.aspx)

Adeliaadelice answered 5/1, 2016 at 17:54 Comment(1)
Could you elaborate a little more? What does this view return and how does it apply to the question?Govern
A
0

If you want to see the IP address of the machine you are running the query on, you can use the query below

select CONNECTIONPROPERTY('client_net_address') AS client_net_address 
Abubekr answered 14/9, 2023 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.