Search

Thursday, February 18, 2016

Query to get IP address of SQL Server

Use below query to get IP address of SQL Server:

DECLARE @IP varchar(40)
DECLARE @IPLine varchar(200)
DECLARE @Pos int

SET NoCount ON
SET @IP = NULL

Create Table #Temp (IPLine VarChar(200))
INSERT #temp EXEC master..xp_cmdshell 'IPconfig'

SELECT TOP 1 @IPLine = IPLine FROM #Temp
WHERE Upper (IPLine) LIKE '%IP ADDRESS%' OR Upper (IPLine) LIKE '%IPV4 ADDRESS%' 
    IF (ISNULL (@IPLine,'***') != '***')
      BEGIN 
       SET @Pos = CharIndex (':',@IPLine,1);
       SET @IP = LTrim(RTrim(SubString (@IPLine , @Pos + 1 ,len (@IPLine) - @Pos)))
      END 
PRINT @IP
DROP TABLE #temp
SET NoCount OFF

If you get below error after executing above query:

Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1
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.

Than you had to enable xp_cmdshell in surface area configuration. and than rerun the above query.

No comments:

Post a Comment