Link to home
Start Free TrialLog in
Avatar of FridaA
FridaAFlag for Iceland

asked on

Get ip address of Delphi client calling a stored procedure

I have a Delphi program which uses Sql Server 2008.  When it calls a stored procedure, I would like to get the ip address of the Delphi client (computer hosting the Delphi program) that called the stored procedure.  Instead of the client himself sending the ip-address as a parameter.  This is for security reasons.
Is this possible?

This doesn't help, it only gives the ip-address of the machine hosting the sql server.
select * from sys.dm_exec_connections
Avatar of ralmada
ralmada
Flag of Canada image

Avatar of FridaA

ASKER

Thanks for the answer.

I can use
declare @host as varchar(20)
SET @host = (select host_name())
select @host

to get the host name, as suggested in your link, but I don't understand how to use the xp_cmdshell 'netstat -an'  command to get the ip-address.  Can I do this in SQL ?  pls provide sql code if you can
Here's an example using netstat -n
For more info on how to use netstat, just run this on the Windows Command Line (CMD): netstat /?
 


 
declare @temptable table (
row varchar(2000)
)
 
declare @getipaddress table (
proto varchar(3),
localaddress varchar(20),
Foreignaddress varchar(20),
state varchar(20)
)
 
insert @temptable 
exec sp_executeSql N'xp_cmdshell  ''netstat -n'''  
 
insert @getipaddress 
select	left(ltrim(row),3) as proto,
		substring(ltrim(row), 8, 19) as localaddress,
		substring(ltrim(row), 31, 19) as Foreignaddress,
		right(row, charindex(' ', reverse(row))-1) as State
from @temptable
where left(ltrim(row),3) = 'TCP'
 
select * from @getipaddress

Open in new window

Avatar of FridaA

ASKER

Sorry for the later answer, I'm still waiting for an answer from the dba.  The problem is that I get an error executing:  
exec sp_executeSql N'xp_cmdshell  ''netstat -n'''

The error is:
SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.

And I'm waiting for the dba to answer if he can do anything, or if this is just not possible (for security reasons).

Not sure what to do in the meantime, I can't really accept a solution when I don't know if it works.  Do I just answer later when I find out?
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FridaA

ASKER

Thank you for this, I will send this to the DBA.  Now he has no excuse, everything done for him :)
Avatar of FridaA

ASKER

Thanks for the (possible) solution.
I'll accept your solution ralmada since I'm sure it works, but the DBA at my company said that it is not an option to activate xp_cmdshell  :/
So I guess I'll have to find another solution, or just not do this at all.
Thanks for your help, and sorry for the late response.
Avatar of FridaA

ASKER

See my last comment about the solution.